NavigationBar bar, tint, and title text color in i

2019-01-12 14:52发布

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()]

}

enter image description here

15条回答
Emotional °昔
2楼-- · 2019-01-12 15:11

//In Swift 4

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
查看更多
贼婆χ
3楼-- · 2019-01-12 15:13

Swift 4

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.barTintColor = UIColor.orange
    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}
查看更多
成全新的幸福
4楼-- · 2019-01-12 15:14

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]
    //nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2
}

enter image description here

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-12 15:14

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.

查看更多
聊天终结者
6楼-- · 2019-01-12 15:14

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
查看更多
该账号已被封号
7楼-- · 2019-01-12 15:17

In Swift 4.1 and Xcode 9.4.1

self.navigationItem.title = "your name"
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
查看更多
登录 后发表回答