Swift
iOS Development
Navigation Bar
UI Customization
SwiftUI

Changing navigation bar color in Swift

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In iOS development, the navigation bar is a critical visual component that guides users through the app's hierarchy. It is often one of the first elements users interact with, and therefore, customizing its appearance, including its color, is a key aspect of creating a cohesive and branded user experience. Swift, as part of iOS development with UIKit, provides developers with a robust set of tools for customizing the navigation bar. This article will guide you through the process of changing the navigation bar color in a Swift-based iOS application.

Understanding Navigation Bar Customization

The UINavigationBar class in UIKit controls the appearance of navigation bars throughout the app. Customizing its appearance usually involves setting its barTintColor for the background and tintColor for the item colors (like buttons and titles).

Primary Properties for Customization

  • barTintColor: This property sets the background color of the navigation bar.
  • tintColor: This property changes the color of item buttons and titles within the navigation bar.
  • titleTextAttributes: This attribute allows customizing the font and color of the title text.

Step-by-Step Guide to Changing Navigation Bar Colors

To change the navigation bar color, you need to modify these properties, usually within the viewDidLoad() method of your view controller. Here’s a step-by-step guide:

Step 1: Setting up the project

Start by launching Xcode and creating a new project. Select the "App" template and continue with standard options to create your Swift-based application.

Step 2: Accessing the Navigation Bar Properties

To customize the navigation bar, you'll access it typically in the view controller that pushes to the navigation stack. Use the navigationController property to access the navigation bar.

Example:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    
4    // Set the navigation bar background color
5    navigationController?.navigationBar.barTintColor = UIColor.systemBlue
6    
7    // Set the navigation bar item color
8    navigationController?.navigationBar.tintColor = UIColor.white
9    
10    // Set the title text attributes
11    navigationController?.navigationBar.titleTextAttributes = [
12        NSAttributedString.Key.foregroundColor: UIColor.white,
13        NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)
14    ]
15}

Step 3: Modifying Global Appearance

If you prefer a consistent look across the entire application, you can configure the appearance of the navigation bar globally in the AppDelegate or SceneDelegate.

Example:

swift
1import UIKit
2
3@UIApplicationMain
4class AppDelegate: UIResponder, UIApplicationDelegate {
5
6    var window: UIWindow?
7
8    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
9        
10        let appearance = UINavigationBar.appearance()
11        appearance.barTintColor = UIColor.systemBlue
12        appearance.tintColor = UIColor.white
13        appearance.titleTextAttributes = [
14            NSAttributedString.Key.foregroundColor: UIColor.white,
15            NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)
16        ]
17        
18        return true
19    }
20}

Additional Details and Customization

Customizing the navigation bar offers more than just color changes. Consider the following enhancements for a richer user experience:

  • Shadow Image: Remove or customize the shadow line under the navigation bar using shadowImage.
  • Translucency: Control whether the navigation bar is translucent using the isTranslucent property.
  • Background Image: Set a background image with setBackgroundImage(_:for:) for a more personalized appearance.

Example with Additional Customizations:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    
4    let navBar = navigationController?.navigationBar
5    
6    // Set a custom shadow image
7    navBar?.shadowImage = UIImage()
8    
9    // Enable transparency
10    navBar?.isTranslucent = true
11    
12    // Set a background image for the bar
13    navBar?.setBackgroundImage(UIImage(named: "customBackgroundImage"), for: .default)
14}

Summary Table

Customization AspectProperty/MethodDescription
Background ColorbarTintColorSets the background color of the navigation bar.
Item ColortintColorChanges the color for icons and text on the bar.
Title Text AttributestitleTextAttributesConfigures the font and color for the title text.
Shadow LineshadowImageCustomizes or removes the shadow line under the bar.
TransparencyisTranslucentControls the translucency of the bar.
Background ImagesetBackgroundImage(_:for:)Sets a custom background image for the bar.

Conclusion

Customizing the navigation bar's color and appearance is a straightforward yet essential task in iOS development, enabling developers to align their app's visual design with the brand's identity and enhance user navigation. By leveraging the properties of the UINavigationBar class, you can create unique and engaging user experiences. Explore further customizations like shadow adjustments and custom background images to fully utilize the navigation bar's capabilities.

With this comprehensive guide, you should be well-equipped to enhance your app’s navigation bar using Swift, providing users with both functionality and aesthetic appeal.


Course illustration
Course illustration

All Rights Reserved.