我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
当前回答
斯威夫特3
被点赞最多的答案是Swift 3不正确。
改变颜色的正确代码是:
self.navigationController?.navigationBar.tintColor = UIColor.white
如果你想改变颜色,改变UIColor。白色以上到所需的颜色
其他回答
如果你试了很多次都没有成功,你可以试试:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red
其实,我试了很多次,才发现这种方法行得通。
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
这段代码具有魔力。而不是红色,改变它作为你的愿望。
在AppDelegate类中使用此代码,在didFinishLaunchingWithOptions中。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .white
}
你应该这样使用:
navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
在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