在 iOS15 中更改 NavigationBar 的当前颜色

Change current colour of NavigationBar in iOS15

我有一个带有自定义颜色主题的应用程序,主题可以在 TableView Header 标签 导航栏 中实现颜色.

执行此操作时,iOS14 运行良好。但是随着 iOS15 的变化,导航栏不能再改变颜色了。透明和 scrollEdgeAppearance 已在 iOS15 中处理,但 NavBar current-colour 在应用 运行(在应用程序(didFinisLaunching)被调用后)时基于用户输入的变化不工作。

我正在使用以下代码在用户选择颜色时触发:

 func setNavBarColour() {
        if #available(iOS 15, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            appearance.backgroundColor = LangBuilds.flavColor
            let proxy = UINavigationBar.appearance()
            proxy.tintColor = LangBuilds.flavColor
            proxy.standardAppearance = appearance
            proxy.scrollEdgeAppearance = appearance
        }
        else {
            self.navigationController?.navigationBar.barTintColor = LangBuilds.flavColor
        }
        self.searchController.searchBar.searchTextField.textColor = .black
        self.searchController.searchBar.searchTextField.backgroundColor = .white
        self.searchController.searchBar.searchTextField.autocapitalizationType = .none
        changeTextHolder()
    }

提前感谢您的帮助。

the NavBar current-colour change based on user input while the app is running(after application(didFinisLaunching) has been called) is not working

当导航栏显示时,您无法使用appearance代理更改导航栏颜色appearance 代理仅影响 future 界面元素。您需要将您的 UINavigationBarAppearance 应用到现有的导航栏 直接:

self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance