Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 836
Hamster Avatar asked Sep 26 '20 05:09

Hamster


People also ask

How do I make a transparent navigation bar in SwiftUI?

You need to do three things to make a navigation bar transparent. Set background image to non-nil empty image ( UIImage() ). Set shadow image to non-nil empty image ( UIImage() ). Set isTranslucent to true .

How do I make a transparent view in SwiftUI?

Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.


1 Answers

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

backup

like image 96
Asperi Avatar answered Dec 15 '22 15:12

Asperi