我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
我试图改变设置按钮的颜色为白色,但不能让它改变。
这两种方法我都试过:
navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
但没有变化,它看起来仍然是这样的:
我怎么把那个按钮变成白色?
当前回答
self.navigationController?.navigationBar.tintColor = UIColor.black // to change the all text color in navigation bar or navigation
self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color
其他回答
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
这适用于我,iOS 9.0+
所有的答案设置UINavigationBar.appearance()。tintColor与苹果在UIAppearance.h中的文档冲突。
注意:在iOS7上tintColor属性已经移动到UIView,现在有特殊的继承行为在UIView.h中描述。 这种继承的行为可能与外观代理发生冲突,因此现在外观代理不允许使用tintColor。
在Xcode中,你需要命令-点击你想要使用外观代理的每个属性来检查头文件,并确保该属性带有ui_appearance ance_selector注释。
因此,通过外观代理将导航栏染成紫色,标题和按钮染成白色的正确方法是:
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().tintColor = .white
注意UIBarButtonItem不是UIView的子类,而是NSObject。它的tintColor属性不是从UIView继承的tintColor。
不幸的是,UIBarButtonItem。tintColor没有使用ui_appearance ance_selector进行注释——但在我看来这是一个文档错误。苹果工程部门对此雷达的回应是支持的。
我更喜欢自定义NavigationController而不是设置全局ui,或者放在ViewController中。
这是我的解决方案
class AppNavigationController : UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
}
}
extension AppNavigationController : UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let backButtonItem = UIBarButtonItem(
title: " ",
style: UIBarButtonItem.Style.plain,
target: nil,
action: nil)
backButtonItem.tintColor = UIColor.gray
viewController.navigationItem.backBarButtonItem = backButtonItem
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
}
}
此外,如果你使用全局设置ui,如UIBarButtonItem.appearance(),你不需要像EKEventEditViewController,PickerViewController等苹果Api。tintColor = .white
你可以用这个。把它放在AppDelegate.swift中。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
return true
}
对于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
}