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

这两种方法我都试过:

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

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

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


当前回答

如果你试了很多次都没有成功,你可以试试:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red

其实,我试了很多次,才发现这种方法行得通。

其他回答

在AppDelegate.swift中的didFinishLaunchingWithOptions函数中添加以下代码

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade

// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]

在swift 2.0使用中

self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();

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

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

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

在AppDelegate类中使用此代码,在didFinishLaunchingWithOptions中。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UINavigationBar.appearance().tintColor = .white

}

在故事板中很容易设置: