NavigationBar栏,色彩和标题文字颜色在iOS 8中
状态栏中的背景文字仍然是黑色的。 如何将颜色更改为白色?
// io8, swift, Xcode 6.0.1 override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.barTintColor = UIColor.blackColor() self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()] }
要通用地改变颜色,这段代码应该位于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
改变它,你必须从ViewController
引用NavigationController
,并在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 = [NSForegroundColorAttributeName : UIColor.white]
对于titleTextAttributes
, 文档说:
您可以为文本属性字典中的标题指定字体,文本颜色,文本阴影颜色和文本阴影偏移量
我喜欢Alex的回答。 如果你想在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] }
要在Objective-C中工作,我必须把下面几行放在我的CustomViewController的viewWillAppear
中。
[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
//在Swift中4
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
用swift更新3
override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.tintColor = UIColor.blue self.navigationController?.navigationBar.barStyle = UIBarStyle.black }
如果要为整个应用程序设置色调和条形颜色,可以将以下代码添加到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被设置
要在故事板(Interface Builder Inspector)中执行此任务
在IBDesignable
帮助下,我们可以添加更多选项到Interface Builder Inspector for UINavigationController
,并在storyboard上调整它们。 首先,将下面的代码添加到您的项目中。
@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 } } }
然后只需在故事板上设置UINavigationController的属性。
对于NavigationBar的自定义颜色标题文本,这里是一个简单和短的代码为Swift 3 UINavigationBar.appearance()。titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
要么
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]