更改 UINavigationController 颜色和字体
Change UINavigationController color and font
我正在这样写我的应用程序路由器:
final class AppRouter {
let navigationController: UINavigationController
init(window: UIWindow) {
navigationController = UINavigationController()
window.rootViewController = navigationController
...
}
我在 application:didFinishLaunchingWithOptions:
方法中调用路由器初始化程序。
我试图通过更改它的属性、子属性来改变它的样式(颜色、字体和其他),使用 UINavigationBar.appearance()
没有任何效果。我将 translucent
设置为 false。只有情节提要更改才会产生任何影响,但我有基于情节提要的导航,这是我不想拥有的。
我看过很多关于这个问题的帖子,没有任何效果。
如果有人有食谱,正在研究最新的 iOS(目前是 11.4),请分享!
编辑:
就像我说的那样进行更改:
UINavigationBar.appearance().barTintColor = color
UINavigationBar.appearance().isTranslucent = false
这是在 didFinishLaunching 中使用的。
或在构造函数中:
navigationController.navigationBar.barTintColor = color
两种方法都无法设置导航控制器栏的颜色。
编辑 2:
应用委托调用:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window
window.makeKeyAndVisible()
appRouter = AppRouter(window: window)
return true
}
使用以下扩展名 UINavigationController
extension UINavigationController
{
func setMainTopNavigationBarAttribute() -> Void
{
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = false
self.navigationBar.barTintColor = UIColor.black
self.navigationBar.tintColor = UIColor.white
self.navigationBar.backgroundColor = UIColor.clear
let navBarAttributesDictionary: [NSAttributedStringKey: Any]? = [
NSAttributedStringKey.foregroundColor: UIColor.black,
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 18.0)
]
self.navigationBar.titleTextAttributes = navBarAttributesDictionary
}
}
final class AppRouter {
let navigationController: UINavigationController
init(window: UIWindow) {
navigationController = UINavigationController()
window.rootViewController = navigationController
navigationController.setMainTopNavigationBarAttribute()
}
我正在这样写我的应用程序路由器:
final class AppRouter {
let navigationController: UINavigationController
init(window: UIWindow) {
navigationController = UINavigationController()
window.rootViewController = navigationController
...
}
我在 application:didFinishLaunchingWithOptions:
方法中调用路由器初始化程序。
我试图通过更改它的属性、子属性来改变它的样式(颜色、字体和其他),使用 UINavigationBar.appearance()
没有任何效果。我将 translucent
设置为 false。只有情节提要更改才会产生任何影响,但我有基于情节提要的导航,这是我不想拥有的。
我看过很多关于这个问题的帖子,没有任何效果。
如果有人有食谱,正在研究最新的 iOS(目前是 11.4),请分享!
编辑:
就像我说的那样进行更改:
UINavigationBar.appearance().barTintColor = color
UINavigationBar.appearance().isTranslucent = false
这是在 didFinishLaunching 中使用的。
或在构造函数中:
navigationController.navigationBar.barTintColor = color
两种方法都无法设置导航控制器栏的颜色。
编辑 2: 应用委托调用:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window
window.makeKeyAndVisible()
appRouter = AppRouter(window: window)
return true
}
使用以下扩展名 UINavigationController
extension UINavigationController
{
func setMainTopNavigationBarAttribute() -> Void
{
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = false
self.navigationBar.barTintColor = UIColor.black
self.navigationBar.tintColor = UIColor.white
self.navigationBar.backgroundColor = UIColor.clear
let navBarAttributesDictionary: [NSAttributedStringKey: Any]? = [
NSAttributedStringKey.foregroundColor: UIColor.black,
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 18.0)
]
self.navigationBar.titleTextAttributes = navBarAttributesDictionary
}
}
final class AppRouter {
let navigationController: UINavigationController
init(window: UIWindow) {
navigationController = UINavigationController()
window.rootViewController = navigationController
navigationController.setMainTopNavigationBarAttribute()
}