Change color of Back button in navigation bar

后端 未结 26 1921
孤街浪徒
孤街浪徒 2020-12-02 05:58

I am trying to change the color of the Settings button to white, but can\'t get it to change.

I\'ve tried both of these:

navigationItem.leftBarButton         


        
相关标签:
26条回答
  • 2020-12-02 06:14

    In Swift3, To set the Back button to red.

    self.navigationController?.navigationBar.tintColor = UIColor.red
    
    0 讨论(0)
  • 2020-12-02 06:15

    In Swift 4, you can take care of this issue using:

    let navStyles = UINavigationBar.appearance()
    // This will set the color of the text for the back buttons.
    navStyles.tintColor = .white
    // This will set the background color for navBar
    navStyles.barTintColor = .black
    
    0 讨论(0)
  • 2020-12-02 06:17

    You can use like this one. Place it inside AppDelegate.swift.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            // Override point for customization after application launch.
    
            UINavigationBar.appearance().translucent = false
            UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
            UINavigationBar.appearance().tintColor = UIColor.whiteColor()
            UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
    
            return true
        }
    
    0 讨论(0)
  • 2020-12-02 06:18

    Swift 5.3:

    UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")
    
    0 讨论(0)
  • 2020-12-02 06:19
    self.navigationController?.navigationBar.tintColor = UIColor.redColor()
    

    This snippet does the magic. Instead of the redColor, change it to as your wish.

    0 讨论(0)
  • 2020-12-02 06:20

    All the answers setting UINavigationBar.appearance().tintColor conflict with Apple's documentation in UIAppearance.h.

    Note for iOS7: On iOS7 the tintColor property has moved to UIView, and now has special inherited behavior described in UIView.h. This inherited behavior can conflict with the appearance proxy, and therefore tintColor is now disallowed with the appearance proxy.

    In Xcode, you need to command-click on each property you want to use with appearance proxy to inspect the header file and make sure the property is annotated with UI_APPEARANCE_SELECTOR.

    So the correct way to color the navigation bar purple and the title and buttons white throughout the app via the appearance proxy is:

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

    Note that UIBarButtonItem is not a subclass of UIView but rather NSObject. So its tintColor property is not the inherited tintColor from UIView.

    Unfortunately, UIBarButtonItem.tintColor is not annotated with UI_APPEARANCE_SELECTOR – but that seems to me a documentation bug. The response from Apple Engineering in this radar states it is supported.

    0 讨论(0)
提交回复
热议问题