我试图改变设置按钮的颜色为白色,但不能让它改变。

这两种方法我都试过:

navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()

但没有变化,它看起来仍然是这样的:

我怎么把那个按钮变成白色?


当前回答

不知道为什么没有人提到这一点…但我所做的正是你在我的viewDidLoad中所做的…但它并没有起作用。然后我把我的代码放在viewWillAppear,它都工作了。

上面的解决方案是改变一个barbuttonItem。如果你想改变代码中每个navigationBar的颜色,那么遵循这个答案。

基本上,使用appearance()更改到类本身就像在应用程序中对该视图的所有实例进行全局更改。更多信息请参阅这里

其他回答

对于Swift 2.0,要更改导航栏的色调颜色,标题文本和后退按钮的色调颜色更改,请使用AppDelegate.swift中的以下命令

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  // Override point for customization after application launch.


    //Navigation bar tint color change

    UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)

    //Back button tint color change

    UINavigationBar.appearance().barStyle = UIBarStyle.Default
    UINavigationBar.appearance().tintColor =  UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)

    //Navigation Menu font tint color change

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent


    return true
}

在Swift3中,将后退按钮设置为红色。

self.navigationController?.navigationBar.tintColor = UIColor.red
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

这段代码具有魔力。而不是红色,改变它作为你的愿望。

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

这适用于我,iOS 9.0+

    self.navigationController?.navigationBar.tintColor = UIColor.black // to change the all text color in navigation bar or navigation 
    self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color