且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-06 08:31:16

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

Maybe you just need to add translucency settings in your SwiftUI view

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

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

alternate is to reset appearance completely, like

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

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

Demo prepared and tested with Xcode 12 / iOS 14