【问题标题】:Change current colour of NavigationBar in iOS15在 iOS 15 中更改导航栏的当前颜色
【发布时间】:2021-11-22 09:43:00
【问题描述】:

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

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

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

 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()
    }

提前感谢您的帮助。

【问题讨论】:

    标签: ios swift uikit ios15


    【解决方案1】:

    在应用程序运行时(在调用 application(didFinisLaunching) 之后)基于用户输入的 NavBar 当前颜色变化不起作用

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

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

    【讨论】:

    • 非常感谢,@matt。对不起,我的愚蠢怀疑,你有义务给我这个答案。
    • 另外,如果我没记错的话,您问题中的代码是直接取自我的stackoverflow.com/a/69302710/341994
    • 是的,你是对的,我有一个时间有限的更新要在星期五之前推送,它是关于解决一个主要错误同时还支持 iOS15 中的主题,所以我拼命尝试 StackOverflow 上的每个解决方案。
    猜你喜欢
    • 2022-01-04
    • 2017-02-17
    • 2021-11-16
    • 1970-01-01
    • 2019-11-13
    • 2013-09-26
    • 2018-02-06
    • 1970-01-01
    相关资源
    最近更新 更多