我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
当前回答
我在swift 5中使用它,并为我工作
navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
其他回答
不知道为什么没有人提到这一点…但我所做的正是你在我的viewDidLoad中所做的…但它并没有起作用。然后我把我的代码放在viewWillAppear,它都工作了。
上面的解决方案是改变一个barbuttonItem。如果你想改变代码中每个navigationBar的颜色,那么遵循这个答案。
基本上,使用appearance()更改到类本身就像在应用程序中对该视图的所有实例进行全局更改。更多信息请参阅这里
在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
斯威夫特5.5
更改完整的应用程序主题
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set for app
UINavigationBar.appearance().tintColor = .white
return true
}
更改特定控制器
let navController = UINavigationController.init(rootViewController: yourViewController)
navController.navigationBar.tintColor = .red
present(navController, animated: true, completion: nil)
Swift 5更新
如果你需要全局设置后退按钮颜色,你可以简单地使用:
UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color
然后你不需要在每个视图控制器上设置后退按钮的背景色。如果你使用这个,你不能在其他视图控制器上设置返回按钮的颜色
BUT
如果您需要在视图控制器上设置后退按钮颜色或在另一个视图控制器上更改,请不要使用上述方法。你可以用:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font:FontFamily.BatonTurbo.medium.font(size: 20),
.foregroundColor: Asset.pureWhite.color] // Naviagtion Title attributes
appearance.backgroundColor = .red // Navigation bar background color
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance
navigationController?.navigationBar.tintColor = .green // Back button color
self.navigationController?.navigationBar.tintColor = UIColor.redColor()
这段代码具有魔力。而不是红色,改变它作为你的愿望。