【问题标题】:How to change bar tint color that covers the notch area on swift (uikit)如何快速更改覆盖缺口区域的条形颜色(uikit)
【发布时间】:2021-09-09 10:30:55
【问题描述】:

我正在尝试将 barTintColorSceneDelegate 更改为,但它并没有覆盖整个顶部区域。看看我的ss:

如何让它覆盖整个顶部区域?

这是我的代码,提前谢谢你。

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }

    UINavigationBar.appearance().barTintColor = UIColor(displayP3Red: 47/225, green: 54/225, blue: 64/225, alpha: 1.0)
    UINavigationBar.appearance().backgroundColor = UIColor(displayP3Red: 47/225, green: 54/225, blue: 64/225, alpha: 1.0)
    UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
}
}

【问题讨论】:

    标签: ios swift uikit navigationbar


    【解决方案1】:

    这应该只需要一行:

    UINavigationBar.appearance().barTintColor = .yellow
    

    【讨论】:

    • 在我的例子中,它在大标题期间没有改变背景颜色,但是当我滚动它时它确实变成了黄色
    • 你有一个覆盖状态栏的自定义视图吗?
    • 找到了,看看我的回答。感谢您的回复。
    【解决方案2】:

    所以,显然是因为 iOS 版本。 Pre iOS 13 只能使用

    UINavigationBar.appearance().barTintColor = .yellow
    

    但现在有点不同了。所以对于这个人,我找到了答案here

    所以我现在要做的是:

        let appearance = UINavigationBarAppearance()
        appearance.backgroundColor = .red
    
        UINavigationBar.appearance().standardAppearance = appearance // for scrolling bg color
        UINavigationBar.appearance().compactAppearance = appearance // not sure why it's here, but u can remove it and still works
        UINavigationBar.appearance().scrollEdgeAppearance = appearance // for large title bg color
    

    这至少对我有用。

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 2017-06-02
      • 2015-10-07
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      相关资源
      最近更新 更多