如何在SwiftUI中制作半透明的导航栏?

时间:2020-09-26 05:38:34

标签: swiftui

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

enter image description here

您可以通过NavBar查看工程图。任何人都知道如何做到这一点,理想情况下是以在明暗模式下工作的方式实现的吗?

1 个答案:

答案 0 :(得分:2)

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

demo1

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

替代方法是完全重置外观,例如

demo2

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

使用Xcode 12 / iOS 14准备并测试了演示

相关问题