我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
当前回答
在swift 2.0使用中
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
其他回答
在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
不知道为什么没有人提到这一点…但我所做的正是你在我的viewDidLoad中所做的…但它并没有起作用。然后我把我的代码放在viewWillAppear,它都工作了。
上面的解决方案是改变一个barbuttonItem。如果你想改变代码中每个navigationBar的颜色,那么遵循这个答案。
基本上,使用appearance()更改到类本身就像在应用程序中对该视图的所有实例进行全局更改。更多信息请参阅这里
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
这段代码具有魔力。而不是红色,改变它作为你的愿望。
斯威夫特5.3:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")
在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()]