【问题标题】:How to make a semi-transparent Navigation Bar in SwiftUI?如何在 SwiftUI 中制作半透明的导航栏?
【发布时间】:2020-09-26 05:38:34
【问题描述】:

我看过几篇讨论如何在 SwiftUI 中制作透明导航栏的帖子,但没有一篇关于如何制作半透明导航栏的帖子,这让我感到惊讶,因为它在 Apple 的默认应用程序中非常常见。例如 Notes 应用:

您可以通过 NavBar 看到绘图。任何人都知道如何做到这一点,最好是在明暗模式下工作?

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    也许你只需要在你的 SwiftUI 视图中添加半透明设置

    init() {
      UINavigationBar.appearance().isTranslucent = true
    }
    

    alternate 是完全重置外观,比如

    init() {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundColor = UIColor.systemBackground.withAlphaComponent(0.5)
        UINavigationBar.appearance().standardAppearance = appearance
    }
    

    使用 Xcode 12 / iOS 14 准备和测试的演示

    【讨论】:

    • 你的第二个解决方案绝对完美,非常非常感谢,我找了好久这个方法!最后一个问题,你把这段代码放在哪里?我把它放在SceneDelegate.scene,但我注意到你写在init(),这是iOS14新的东西吗?
    • 我把它放在 ContentView 的 init 中只是为了演示,你可以把它放在任何适合你的地方,但在 NavigationView 创建之前。
    • 第二个解决方案很有魅力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多