IOS8中的导航条、色彩和标题文本颜色

状态栏中的背景文本仍为黑色。如何将颜色更改为白色?

// io8, swift, Xcode 6.0.1
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]


}

enter image description here

252500 次浏览

要全面改变颜色,这段代码应该放在 NavigationControllerviewDidLoad函数中:

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,并在该 ViewControllerviewWillAppear函数中编写类似的代码行。

我喜欢亚历克斯的回答。如果你想在 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
}

enter image description here

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医生表示:

可以指定字体、文本颜色、文本阴影颜色和文本 文本属性字典中标题的阴影偏移量

要在 目标 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

更新迅捷4

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}

如果您想为整个应用程序设置色调颜色和条形图颜色,可以将以下代码添加到

 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 督察)

IBDesignable的帮助下,我们可以为 Interface Builder 增加更多的选项,并在故事板上调整它们。首先,向项目中添加以下代码。

@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 的属性。

enter image description here

对于自定义颜色到 NavigationBarTitleText,这里有一个简单而简短的代码,用于 Swift 3:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

或者

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName :UIColor.white]

Swift 5

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]


Swift 4

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

Swift 4

override func viewDidLoad() {
super.viewDidLoad()


navigationController?.navigationBar.barTintColor = UIColor.orange
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}

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
}

在 Swift 3中,这种方法是有效的:

navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]

在 Swift5和 Xcode 10中

self.navigationItem.title = "your name"
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes

在 Swift 4.2中

var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]

迅捷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]

在 Swift 版本4.2中,将导航栏标题的文本颜色设置为白色:

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

通过 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 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]