状态栏中的背景文本仍为黑色.如何将颜色更改为白色?
// io8, swift, Xcode 6.0.1 override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.barTintColor = UIColor.blackColor() self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()] }
我喜欢亚历克斯的回答.如果你想快速尝试一下,ViewController
确保你使用
viewWillAppear() override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) var nav = self.navigationController?.navigationBar nav?.barStyle = UIBarStyle.Black nav?.tintColor = UIColor.white nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange] //nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2 }
要普遍地改变颜色,此代码应坐在NavigationController
的viewDidLoad
功能:
class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate { override func viewDidLoad() { super.viewDidLoad() // Status bar white font self.navigationBar.barStyle = UIBarStyle.Black self.navigationBar.tintColor = UIColor.whiteColor() } }
若要更改每个ViewController
你就必须引用NavigationController
从ViewController
写在类似线ViewController
的viewWillAppear
功能.
在AppDelegate.swift
,application(_:didFinishLaunchingWithOptions:)
我把以下内容:
UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0) UINavigationBar.appearance().tintColor = UIColor.white UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
对于NSAttributedStringKey
,文档说:
您可以在文本属性字典中为标题指定字体,文本颜色,文本阴影颜色和文本阴影偏移
//在Swift 4中
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
要在objective-c中工作,我必须viewWillAppear
在我的CustomViewController中添加以下行.
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]]; [self.navigationController.navigationBar setTranslucent:NO];
对于Swift2.x,这适用于:
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
对于Swift3.x,这适用于:
self.navigationController?.navigationBar.barTintColor = UIColor.red
如果要为整个应用程序设置色调颜色和条形颜色,可以将以下代码添加到AppDelegate.swift中
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. var navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.tintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0) navigationBarAppearace.barTintColor = UIColor(red:0.76, green:0.40, blue:0.40, alpha:1.0) navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] return true `
导航barTintColor和tintColor已设置
在storyboard中完成此工作(Interface Builder Inspector)
借助于IBDesignable
我们,我们可以为Interface Builder Inspector添加更多选项,UINavigationController
并在故事板上进行调整.首先,将以下代码添加到项目中.
@IBDesignable extension UINavigationController { @IBInspectable var barTintColor: UIColor? { set { navigationBar.barTintColor = newValue } get { guard let color = navigationBar.barTintColor else { return nil } return color } } @IBInspectable var tintColor: UIColor? { set { navigationBar.tintColor = newValue } get { guard let color = navigationBar.tintColor else { return nil } return color } } @IBInspectable var titleColor: UIColor? { set { guard let color = newValue else { return } navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: color] } get { return navigationBar.titleTextAttributes?["NSForegroundColorAttributeName"] as? UIColor } } }
然后只需在storyboard上设置UINavigationController的属性.
在Swift 4.1和Xcode 9.4.1中
self.navigationItem.title = "your name" let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white] navigationController?.navigationBar.titleTextAttributes = textAttributes
更新了swift 4
override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.tintColor = UIColor.blue self.navigationController?.navigationBar.barStyle = UIBarStyle.black }