I am building a SwiftUI app where I have an overlay that is conditionally shown across my entire application like this:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
.safeAreaInset(edge: .bottom) {
Group {
if myCondition {
EmptyView()
} else {
OverlayView()
}
}
}
}
}
}
I would expect this to adjust the safe area insets of the NavigationView and propagate it to any content view, so content is not stuck under the overlay. At least that's how additionalSafeAreaInsets in UIKit would behave. Unfortunately, it seems that SwiftUI ignores any safeAreaInsets() on a NavigationView (the overlay will show up, but safe area is not adjusted).
While I can use a GeometryReader to read the overlay size and then set safeAreaInsets() on ContentView, this will only work for ContentView - as soon as I navigate to the next view the safe area is gone.
Is there any nice way to get NavigationView to accept additional safe area insets, either by using safeAreaInsets() or by some other way?
So it seems
NavigationViewdoes not adjust its safe area inset when using.safeAreaInset. If this is intended or a bug is not clear to me. Anyway, I solved this for now like this (I wanted to use pure SwiftUI, using UIKit'sadditionalSafeAreaInsetsmight be an option to):Main App File:
EVERY Content View that is pushed on your NavigationView:
Why does it work?
GeometryReaderis used to read the size of the overlay created insidesafeAreaInset(). The size is written to the sharedSafeAreaControllerSafeAreaControlleris handed as anEnvironmentObjectto every content view of our navigation.safeAreaInsetof every content view with the height read from theSafeAreaController- this will basically create an invisible bottom safe area that is the same size as our overlay, thus making room for the overlay