ios - 更改 iOS 15 中导航栏的当前颜色

标签 ios swift uikit ios15

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

执行此操作时,它在 iOS14 上运行良好。但是随着iOS15的变化,导航栏不能再变色了。透明和 scrollEdgeAppearance 已在 iOS15 中处理,但在应用程序运行时(在调用 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()
    }

提前感谢您的帮助。

最佳答案

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

关于ios - 更改 iOS 15 中导航栏的当前颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69394073/

相关文章:

ios - 离开 View 时停止 NSTimer

ios - UICollectionView 的索引路径日期

ios - 如何从模态 XIB 设置 VC 的背景颜色

ios - wait_fences : failed to receive reply: 10004003?

iphone - 从 UINavigationController 堆栈弹出时,UITableViewController 是否有特殊行为?

cocoa-touch - 检查 UIView 是否被触摸?

ios - 如何在 Storyboard 中使 UiView 尊重百分比

ios - 如何在 iOS/iPhone/iPad 上获取 WiFi 加密模式?

ios - 在 Xcode 中滚动时固定导航栏

swift - 在 Swift 中按下按钮时将对象重置为固定 (x,y) 坐标