首页 > 解决方案 > 使用外观代理的全局更改 UINavigationBar 背景不起作用

问题描述

我正在尝试使用 UINavigationBarAppearance 更改我的应用程序中所有 UINavigationBars 的一些属性。我application(_:didFinishLaunchingWithOptions:)在 AppDelegate.swift 中调用以下函数:

func customizeAppearance() {

    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.configureWithDefaultBackground()
    navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    navBarAppearance.backgroundColor = UIColor.black

    UITabBar.appearance().backgroundColor = UIColor.black

    let tintColor = UIColor(red: 255/255.0, green: 238/255.0, blue: 136/255.0, alpha: 1.0)
    UITabBar.appearance().tintColor = tintColor

}

我希望这会改变标题文本的背景颜色和颜色,但是当我运行应用程序时,只有文本颜色会改变(UITabBar 也是如此)。有什么建议么?

标签: swiftuikitios13

解决方案


你应该UINavigationBarAppearance在你的 UINavigationControllers 中使用它。如果你使用 UINavigationController 的全局外观,你应该使用UINavigationBar.appearance()而不是UINavigationBarAppearance().


推荐阅读