Changing the Tint Color of UIBarButtonItem

后端 未结 8 1862
暗喜
暗喜 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:33

    This worked for me

    AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
    
    0 讨论(0)
  • 2020-12-15 03:36
    UITabBar.appearance().tintColor = UIColor.yellowColor()
    
    0 讨论(0)
  • 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
    }
    

    0 讨论(0)
  • 2020-12-15 03:44

    Swift 5

    barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
    
    0 讨论(0)
  • 2020-12-15 03:46

    In iOS 7, to set the color of all barButtonItems in your app, set the tintColor property on the application's window in the AppDelegate.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window.tintColor = [UIColor whiteColor];
        return YES;
    }
    

    More detailed info in Apple's iOS 7 UI Transition Guide (Specifically under the 'Using Tint Color` section).

    ***OR***

    Based on some of the comments, you can also achieve this with the UINavigationBar appearance proxy. This will affect the tintColor of only UIBarButtonItems, as opposed to setting the tintColor on the window and affecting all subviews of that window.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
            [UINavigationBar appearance].tintColor = [UIColor whiteColor];
        }
    
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-15 03:47

    I think you are looking for a property of your UINavigationBar. Try setting self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    See "Appearance of Navigation Bars" section: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1

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