Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106k views
in Technique[技术] by (71.8m points)

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

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:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

demo1

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

alternate is to reset appearance completely, like

demo2

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...