Changing the Tint Color of UIBarButtonItem

后端 未结 8 1884
暗喜
暗喜 2020-12-15 03:31

I have a project using Storyboards and whenever I push a view controller with a segue, the dynamically created bar button item is always blue.

8条回答
  •  眼角桃花
    2020-12-15 03:38

    In Swift 3.0

    let navigationBarAppearnce = UINavigationBar.appearance()
    

    A navigation bar’s tintColor affects the color of the back indicator image, button titles, and button images.

    navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
    

    The barTintColor property affects the color of the bar itself

    navigationBarAppearnce.tintColor = UIColor.white
    

    Final Code

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
     let navigationBarAppearnce = UINavigationBar.appearance()
    
     navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
    
     navigationBarAppearnce.tintColor = UIColor.white
    
     navigationBarAppearnce.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    
     //Change status bar color
     UIApplication.shared.statusBarStyle = .lightContent
    
     return true
    }
    

提交回复
热议问题