我做了一个自定义的 UINavigationBar,我想在全局范围内使用它。我在其他示例中看到,要全局设置 UINavigationBar 外观,您必须在 AppDelegate 中执行类似 let navigationBar = UINavigationBar.appearance()
的操作。然后你可以设置属性。在我的例子中,我已经在 class var
中设置了所有属性,如下所示:
extension UINavigationBar {
class var customNavBar : UINavigationBar{
let navigationBar = UINavigationBar()
navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir-Black", size: 40)!, NSForegroundColorAttributeName: UIColor.RED]
//gets rid of black separation bar
navigationBar.shadowImage = UIImage()
navigationBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
//set the background color to white
navigationBar.tintColor = UIColor.white
navigationBar.isTranslucent = false
return navigationBar
}
}
现在,我如何将 AppDelegate 中的 UINavigationBar 设置为固有的这些属性。我的意思是在应用程序委托(delegate)中有这样的东西。或者类似的当然。 在 AppDelegate 中:
let navigationBar = UINavigationBar.customNavBar.appearance()
我想这样做的原因是因为我有一些其他的 UIViewControllers
(不从我的 TabViewController
继续),我想在其中手动添加一个 UINavigationBar,它必须看起来像自定义的一样。
最佳答案
如果你想定制你的导航栏到那个程度,你最好创建一个子类。问题是您需要手动将类名传递给需要使用它的任何导航 Controller 。如果需要,您可以对 UIToolbar
执行相同的操作,或者传递 nil
以使用默认值。
class MyNavigationBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
// configure your nav bar here
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let navController = UINavigationController(navigationBarClass: MyNavigationBar.self, toolbarClass: nil)
供引用:https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621866-init
关于ios - 来自 AppDelegate 的自定义 UINavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48370273/