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

前端 未结 18 2174
星月不相逢
星月不相逢 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:48

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

    (For Swift 4 or earlier use NSAttributedStringKey instead of NSAttributedString.Key)

    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

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

    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.

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

    Swift 5

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

    Swift 4

    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    
    0 讨论(0)
  • 2020-12-02 04:50

    In Swift 3 this works:

    navigationController?.navigationBar.barTintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
    
    0 讨论(0)
  • 2020-12-02 04:52

    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

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

    in Swift 4.2

    var nav = self.navigationController?.navigationBar
    nav?.barStyle = UIBarStyle.Black
    nav?.tintColor = UIColor.white
    nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
    
    0 讨论(0)
提交回复
热议问题