可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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()] }
回答1:
In AppDelegate.swift
, in application(_:didFinishLaunchingWithOptions:)
I put the following:
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 = [NSAttributedStringKey.foregroundColor : UIColor.white]
For titleTextAttributes
, the docs say:
You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary
回答2:
I like Alex's answer. If you want something quick to try out in a ViewController
make sure you use
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] }
回答3:
To change the color universally, this code should sit in the NavigationController
's viewDidLoad
function:
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.
回答4:
To work in objective-c I have to put the following lines in viewWillAppear
in my CustomViewController.
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]]; [self.navigationController.navigationBar setTranslucent:NO];
For Swift2.x this works:
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
For Swift3.x this works:
self.navigationController?.navigationBar.barTintColor = UIColor.red
回答5:
//In Swift 4
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
回答6:
To do this job in storyboard (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 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.

回答7:
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
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
回答8:
Updated with swift 3
override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.tintColor = UIColor.blue self.navigationController?.navigationBar.barStyle = UIBarStyle.black }
回答9:
Swift 4
override func viewDidLoad() { super.viewDidLoad() navigationController?.navigationBar.barTintColor = UIColor.orange navigationController?.navigationBar.tintColor = UIColor.white navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] }
回答10:
Swift 4.1
Add a func to viewDidLoad
override func viewDidLoad() { super.viewDidLoad() setup() }
In the setup()
function add:
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:
In Swift 3 this works:
navigationController?.navigationBar.barTintColor = UIColor.white navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
回答12:
For custom color to TitleText
at NavigationBar
, here a simple and short code for Swift 3:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
or
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName :UIColor.white]