iOS 8 中的导航栏栏、色调和标题文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26008536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NavigationBar bar, tint, and title text color in iOS 8
提问by AG1
The background text in the status bar is still black. How do I change the color to white?
状态栏中的背景文字仍然是黑色的。如何将颜色更改为白色?
// io8, swift, Xcode 6.0.1
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]
}
回答by Albert Vila Calvo
In AppDelegate.swift
, in application(_:didFinishLaunchingWithOptions:)
I put the following:
在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]
(For Swift 4 or earlier use NSAttributedStringKey
instead of NSAttributedString.Key
)
(对于 Swift 4 或更早版本使用NSAttributedStringKey
而不是NSAttributedString.Key
)
For titleTextAttributes
, the docssay:
对于titleTextAttributes
,文档说:
You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary
可以在文本属性字典中为标题指定字体、文本颜色、文本阴影颜色和文本阴影偏移
回答by AG1
I like Alex's answer. If you want something quick to try out in a ViewController
make sure you use
我喜欢亚历克斯的回答。如果你想快速尝试一些东西,请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
}
回答by Alex
To change the color universally, this code should sit in the NavigationController
's viewDidLoad
function:
要普遍更改颜色,此代码应位于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()
}
}
To change it per ViewController
you would have to reference the NavigationController
from the ViewController
and write similar lines in that ViewController
's viewWillAppear
function.
要更改它,ViewController
您必须引用NavigationController
fromViewController
并在 thatViewController
的viewWillAppear
函数中编写类似的行。
回答by Flower
Swift 5
斯威夫特 5
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Swift 4
斯威夫特 4
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
回答by Niko Klausnitzer
To work in objective-cI have to put the following lines in viewWillAppear
in my CustomViewController.
要在objective-c 中工作,我必须viewWillAppear
在我的CustomViewController 中添加以下几行。
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];
For Swift2.xthis works:
对于Swift2.x,这有效:
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
For Swift3.xthis works:
对于Swift3.x,这有效:
self.navigationController?.navigationBar.barTintColor = UIColor.red
回答by Fangming
To do this job in storyboard (Interface Builder Inspector)
在故事板(Interface Builder Inspector)中完成这项工作
With help of IBDesignable
, we can add more options to Interface Builder Inspector for UINavigationController
and tweak them on storyboard. First, add the following code to your project.
在 的帮助下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
}
}
}
Then simply set the attributes for UINavigationController on storyboard.
然后只需在情节提要上设置 UINavigationController 的属性。
回答by VirajP
If you want to set the tint color and bar color for the entire app, the following code can be added to AppDelegate.swift in
如果要为整个应用设置色调颜色和条形颜色,可以在 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
`
回答by Raj Joshi
Updated with swift 4
用 swift 4 更新
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}
回答by iOS
In Swift5 and Xcode 10
在 Swift5 和 Xcode 10 中
self.navigationItem.title = "your name"
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
回答by Abhishek Jain
Swift 4.2 version of Albert's answer-
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]