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

这两种方法我都试过:

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

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

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


当前回答

你可以用这个。把它放在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
    }

其他回答

斯威夫特3

被点赞最多的答案是Swift 3不正确。

改变颜色的正确代码是:

self.navigationController?.navigationBar.tintColor = UIColor.white

如果你想改变颜色,改变UIColor。白色以上到所需的颜色

在Swift 4中,你可以使用以下方法来解决这个问题:

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

在故事板中很容易设置:

你应该这样使用:

navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white

它将通过-(void)viewDidLoad中的这一行来解决:

self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;