状态栏中的背景文本仍然是黑色的。如何将颜色更改为白色?
// io8, swift, Xcode 6.0.1
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]
}
https://i.stack.imgur.com/LrELQ.png
在 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]
(对于 Swift 4 或更早版本,使用 NSAttributedStringKey
而不是 NSAttributedString.Key
)
对于 titleTextAttributes
,docs 说:
您可以在文本属性字典中为标题指定字体、文本颜色、文本阴影颜色和文本阴影偏移量
我喜欢亚历克斯的回答。如果您想在 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
}
https://i.stack.imgur.com/54vY0.png
NSForegroundColorAttributeName
现在应该是 NSAttributedStringKey.foregroundColor
NSAttributedString.Key.foregroundColor
而不是 NSForegroundColorAttributeName
要普遍更改颜色,此代码应位于 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
函数中编写类似的行。
斯威夫特 5
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
斯威夫特 4
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
要在 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
在情节提要中完成这项工作(Interface Builder Inspector)
在 IBDesignable
的帮助下,我们可以为 UINavigationController
添加更多选项到 Interface Builder Inspector 并在情节提要上调整它们。首先,将以下代码添加到您的项目中。
@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 的属性。
https://i.stack.imgur.com/ZJsJX.gif
如果要设置整个app的tint color和bar color,可以在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
`
Navigation barTintColor and tintColor is set
用 swift 4 更新
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}
在 Swift5 和 Xcode 10 中
self.navigationItem.title = "your name"
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
斯威夫特 5.1
仅在 ViewDidLoad()
中复制和粘贴,并根据需要更改其大小。在复制和粘贴之前,在屏幕顶部添加导航栏。
navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "TitilliumWeb-Bold.ttf", size: 16.0)!, NSAttributedString.Key.foregroundColor: UIColor.white]
如果它不起作用,那么您可以尝试仅更改其文本颜色
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Swift 4.2 版本的 Albert 的回答——
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 = [.foregroundColor : UIColor.white]
在 Swift 4.2 版本中将导航栏标题的文本颜色设置为白色:
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
斯威夫特 4
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.barTintColor = UIColor.orange
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}
斯威夫特 4.1
添加一个函数到 viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
在 setup()
函数中添加:
func setup() {
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.barStyle = .blackOpaque
navigationItem.title = "YOUR_TITLE_HERE"
navigationController?.navigationBar.barTintColor = .black
let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navigationController?.navigationBar.largeTitleTextAttributes = attributes
}
对于 NavigationBar
处的 TitleText
自定义颜色,这里有一个 Swift 3 的简单而简短的代码:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
或者
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName :UIColor.white]
在斯威夫特 4.2
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
Swift 3.2(不是 Swift 4.0)
self.navigationController?.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
// unconfirmed but I assume this works:
self.navigationController?.navigationBar.barTintColor = UIColor.white
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
在 Swift 3 中这有效:
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]