我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
当前回答
让我们试试这段代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.whiteColor() // Back buttons and such
navigationBarAppearace.barTintColor = UIColor.purpleColor() // Bar's background color
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] // Title's text color
self.window?.backgroundColor = UIColor.whiteColor()
return true
}
其他回答
这段代码更改箭头的颜色
self.navigationController.navigationBar.tintColor = UIColor.whiteColor();
如果这不起作用,请使用下面的代码:
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
Swift 3 Notes
UIColor.whiteColor()和类似的被简化为UIColor.white
此外,许多以前的隐式可选项已被更改为显式,因此您可能需要:
self.navigationController?.navigationBar =
如果你在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 5中使用它,并为我工作
navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
对于Swift 2.0,要更改导航栏的色调颜色,标题文本和后退按钮的色调颜色更改,请使用AppDelegate.swift中的以下命令
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Navigation bar tint color change
UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)
//Back button tint color change
UINavigationBar.appearance().barStyle = UIBarStyle.Default
UINavigationBar.appearance().tintColor = UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)
//Navigation Menu font tint color change
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
return true
}
斯威夫特3
被点赞最多的答案是Swift 3不正确。
改变颜色的正确代码是:
self.navigationController?.navigationBar.tintColor = UIColor.white
如果你想改变颜色,改变UIColor。白色以上到所需的颜色