2014-09-24 120 views
111

状态栏中的背景文字仍然是黑色。如何将颜色更改为白色?iOS 8中的NavigationBar栏,色调和标题文字颜色

// 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

+0

谢谢!它工作很好。 – Raja 2017-08-21 09:52:06

回答

214

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 = [NSAttributedStringKey.foregroundColor : UIColor.white] 

对于titleTextAttributes中, docs说:

您可以指定字体,文本颜色,文字阴影颜色,文字 阴影在文本标题偏移属性字典

+2

如果我们有多个导航栏怎么办? – sumit 2016-04-12 03:58:44

+5

tintColor不适用于Xcode 7.3.1。文本仍然是黑色的。 – triiiiista 2016-05-26 13:47:19

+1

谢谢你说在哪里放置代码。这帮助了很多。 – krummens 2016-06-09 20:25:25

78

普遍地改变颜色,此代码应坐在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你就必须引用NavigationController从在ViewControllerviewWillAppear函数中写出类似的行。

+0

如何覆盖NavigationController以使用这些方法? – AG1 2014-09-24 04:54:23

+0

您不重写NavigationController,只需在ViewDidLoad函数中对其进行修改即可。我上面更新了我的答案。 – Alex 2014-09-24 04:55:26

+1

那么开发人员只需将NavigationController.swift添加到工作区?如果是那么整洁! – AG1 2014-11-07 03:16:49

85

我喜欢Alex的回答。如果你想要快速尝试在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] 
} 

enter image description here

+1

我不工作,仍然显示我在黑色 – 2014-11-05 15:51:53

+0

但我使用ISO 8.1 – 2014-11-05 15:52:44

+1

为什么viewWillAppear()?我在viewDidLoad中这样做,它工作正常。 – 2015-07-12 13:29:28

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 
+0

但是当我回到另一个视图控制器时,颜色也发生了变化。任何想法如何防止这一点。我希望我的主视图控制器是一种颜色,其他颜色。 – user3163404 2015-08-24 16:55:31

+1

我想你也必须在你的新视图控制器中设置颜色。 – 2015-08-28 08:07:57

3

更新与SWIFT 3

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

如果您想为整个应用着色颜色和条颜色,下面的代码可以被添加到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

6

要在故事板做好这项工作(界面Builder Inspector)

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 
     } 
    } 
} 

然后只需在故事板上设置UINavigationController的属性即可。

enter image description here

0

对于自定义颜色TitleTextNavigationBar,这里夫特3简单且短码:

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

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

//在夫特4

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

我得到:Type'NSAttributedStringKey'(aka'NSString')没有成员'foregroundColor' – Alfi 2017-12-10 12:39:40

2

斯威夫特4

override func viewDidLoad() { 
    super.viewDidLoad() 

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

雨燕4.1

添加FUNC按键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 
    } 
0

在斯威夫特3本作品:

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