要在故事板做好這項工作(界面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的屬性即可。
謝謝!它工作很好。 – Raja 2017-08-21 09:52:06