状态栏中的背景文字仍为黑色。如何将颜色更改为白色?
// io8, swift, Xcode 6.0.1
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]
}
答案 0 :(得分:281)
在 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说:
您可以指定字体,文本颜色,文本阴影颜色和文本 文本属性字典中标题的阴影偏移
答案 1 :(得分:110)
我喜欢亚历克斯的回答。如果您想在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
}
答案 2 :(得分:81)
要普遍改变颜色,此代码应位于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
中写出类似的行功能。
答案 3 :(得分:27)
//在Swift 4中
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
答案 4 :(得分:16)
要在 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
答案 5 :(得分:11)
在故事板(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的属性。
答案 6 :(得分:7)
如果要为整个应用设置色调颜色和条形颜色,可以将以下代码添加到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
`
答案 7 :(得分:5)
更新了swift 4
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}
答案 8 :(得分:5)
在Swift 4.1和Xcode 9.4.1中
self.navigationItem.title = "your name"
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
答案 9 :(得分:3)
Swift 4
let el = await page.$x('//*[@id="readium-right-panel"]/ul/li[1]');
答案 10 :(得分:3)
Swift 4.1
向viewDidLoad添加func
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
}
答案 11 :(得分:2)
在Swift 4.2版中将导航栏标题的文本颜色设置为白色:
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
答案 12 :(得分:2)
Swift 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]
答案 13 :(得分:1)
对于TitleText
处NavigationBar
的自定义颜色,这里是Swift 3的简单和简短代码:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
或
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName :UIColor.white]
答案 14 :(得分:1)
阿尔伯特答案的快速4.2版本-
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]
答案 15 :(得分:0)
在Swift 3中,这有效:
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
答案 16 :(得分:0)
在Swift 4.2中
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
答案 17 :(得分: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