NavigationBar bar, tint, and title text color in iOS 8

前端 未结 18 2175
星月不相逢
星月不相逢 2020-12-02 04:08

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         


        
相关标签:
18条回答
  • 2020-12-02 04:35

    Swift 4.2 version of Albert's answer-

    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]
    
    0 讨论(0)
  • 2020-12-02 04:35

    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]
    
    0 讨论(0)
  • 2020-12-02 04:37

    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.

    0 讨论(0)
  • 2020-12-02 04:37

    Swift up through Swift 3.2 (not 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
    
    0 讨论(0)
  • 2020-12-02 04:38

    Swift 5.1

    Only copy and Paste in ViewDidLoad() and Change its and size as your need. Before copy and paste add Navigation Bar on top of the Screen.

    navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "TitilliumWeb-Bold.ttf", size: 16.0)!, NSAttributedString.Key.foregroundColor: UIColor.white]
    
    

    If it not work then you can try for only change its text color

    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
    0 讨论(0)
  • 2020-12-02 04:44

    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
    
    0 讨论(0)
提交回复
热议问题