Change the color of iOS Navigation Bar

85,596

Solution 1

In fact, i found that the solution was to use in the AppDelegate.siwft :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 0/255, blue: 205/255, alpha: 1)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

    return true
}

and then in each view controller, that we need another background color or something else

  1. the segue should be different than "show"

  2. use the func viewWillAppear

     override func viewWillAppear(animated: Bool) {
         super.viewWillAppear(animated)
         self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
         self.navigationController?.navigationBar.tintColor = UIColor.blueColor()
         self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blueColor()]
    }
    

Solution 2

Updated for Swift 3

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

Swift 4

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

Swift 5

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false

Solution 3

Swift 4.2:

    //To change Navigation Bar Background Color
    UINavigationBar.appearance().barTintColor = UIColor.blue
    //To change Back button title & icon color
    UINavigationBar.appearance().tintColor = UIColor.white
    //To change Navigation Bar Title Color
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

Swift 3.x:

//To change Navigation Bar Background Color
UINavigationBar.appearance().barTintColor = UIColor.blue
//To change Back button title & icon color
UINavigationBar.appearance().tintColor = UIColor.white
//To change Navigation Bar Title Color
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

Solution 4

For changing the navigation bar theme color throughout the full app you can do this by using UiNavigation bar appearance.

UINavigationBar.appearance().barTintColor = UIColor.redColor()

Solution 5

Make the following update to the AppDelegate.Swift file i.e. UINavigationBar.appearance().barTintColor = UIColor(red:x.xx, green:x.xx, blue:x.xx, alpha:1.0)

Refer to example below

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    UINavigationBar.appearance().barTintColor = UIColor(red:0.03, green:0.25, blue:0.11, alpha:1.0)
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}
Share:
85,596
Anthony Shahine
Author by

Anthony Shahine

Updated on January 12, 2022

Comments

  • Anthony Shahine
    Anthony Shahine over 2 years

    I'm trying to change the color of my navigator bar but I found that it's only impossible if the navigator is the root one.

    I'm trying this:

    self.navigationController?.navigationBar.translucent = true
    
    self.navigationController!.navigationBar.barTintColor = UIColor.blueColor()
    

    All my Viewcontrollers are related to navigator controllers. However nothing is changed. In fact I tried to make the same things from storyboard but it works only if I'm in the first navigator.

    I tried to read everything related to this problem but found nothing

    I could add any item to the navigator bar like this

    let HomeImage = UIImage(named: "home")!
        let Home : UIBarButtonItem = UIBarButtonItem(image: HomeImage,  style: .Plain, target: self, action: "home:")
        navigationItem.rightBarButtonItem = Home
    
  • TheSwiftGuy77
    TheSwiftGuy77 about 6 years
    do i have to do this in appdelegate.siwft file ?
  • iAj
    iAj about 6 years
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // setup navBar..... }
  • iAj
    iAj about 6 years
    @PrashannaChudal yes, u can.. code this in above appdelegate method (see above comment)..
  • Codetard
    Codetard about 6 years
    It works, but it doesn't makes seamless transition between two controller
  • Takasur
    Takasur about 6 years
    Use of unresolved identifier 'NSForegroundColorAttributeName'
  • Dekel Maman
    Dekel Maman almost 6 years
    @Takasur for swift 4 use, NSAttributedStringKey.foregroundColor
  • Wei WANG
    Wei WANG almost 6 years
    And also make sure you don't have a background image (which covers the background color) in your navigation bar. I had mine there accidentally set by someone else and it cost me couple hours to check it out...
  • 鸡肉味嘎嘣脆
    鸡肉味嘎嘣脆 over 5 years
    @Anthony I got an other problem. This successful change the title text color. But if push another view controller then return back, the title text color still not changed.
  • cumanzor
    cumanzor over 5 years
    Swift 4.2, use NSAttributedString.Key.foregroundColor instead.
  • Vilmir
    Vilmir almost 5 years
    For some weird reason, the auto-completion in Xcode 10 was not proposing barTintColor as a property of UINavigationBar.appearance(), but copy/pasting your code did compile and work. Thanks!
  • Muhammad Shauket
    Muhammad Shauket over 4 years
    this only change style, not bar color. :)