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

这两种方法我都试过:

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

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

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


当前回答

如果你在Settings视图控制器中已经有了后退按钮,你想改变Payment Information视图控制器上后退按钮的颜色为其他颜色,你可以在Settings视图控制器的prepareforsegue中这样做:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "YourPaymentInformationSegue"
    {
        //Make the back button for "Payment Information" gray:
        self.navigationItem.backBarButtonItem?.tintColor = UIColor.gray               
    }
}

其他回答

在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
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

这段代码具有魔力。而不是红色,改变它作为你的愿望。

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

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

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

在Swift3中,将后退按钮设置为红色。

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

斯威夫特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)