How to set font & color of the title in UINavigationBar using iOS5 appearance API?

后端 未结 7 1319
北恋
北恋 2020-12-04 10:50

I have a multiple View Controllers and I want to set the font color of all to red.

 [[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]]         


        
7条回答
  •  渐次进展
    2020-12-04 11:32

    If you need to do this in Swift, you can create an extension for the UINavigationBar to allow you to get or set these settings.

    extension UINavigationBar {
    var titleColor: UIColor? {
        get {
            if let attributes = self.titleTextAttributes {
                return attributes[NSForegroundColorAttributeName] as? UIColor
            }
            return nil
        }
        set {
            if let value = newValue {
                self.titleTextAttributes = [NSForegroundColorAttributeName: value]
            }
        }
    }
    
    var titleFont: UIFont? {
        get {
            if let attributes = self.titleTextAttributes {
                return attributes[NSFontAttributeName] as? UIFont
            }
            return nil
        }
        set {
            if let value = newValue {
                self.titleTextAttributes = [NSFontAttributeName: value]
            }
        }
        }
    }
    

    You can then set the color and font like this:

    navigationBar.titleColor = UIColor.redColor()
    navigationBar.titleFont = UIFont.systemFontOfSize(12)
    

提交回复
热议问题