How to make a semi-transparent Navigation Bar in SwiftUI?

三世轮回 提交于 2021-01-19 05:57:53

问题


I've seen several posts discussing how to make a transparent Navbar in SwiftUI, but none on how to make a semi-transparent one, which is surprising to me as its a very common patter on Apple's default apps. For example the Notes app:

You can see the drawing through the NavBar. Anyone know how to do this, ideally in a way which works in light/dark mode?


回答1:


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
}

Demo prepared and tested with Xcode 12 / iOS 14



来源:https://stackoverflow.com/questions/64074260/how-to-make-a-semi-transparent-navigation-bar-in-swiftui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!