I have 2 views:
import SwiftUI
struct SwiftUIView: View {
var body: some View {
NavigationStack {
... //show SwiftUIView2
}
.toolbar {
...
}
.overlay {
... //fullscreen overlay
}
}
}
#Preview {
SwiftUIView()
}
import SwiftUI
struct SwiftUIView2: View {
var body: some View {
NavigationStack {
...
}
.toolbar {
...
}
.overlay {
... //fullscreen overlay
}
}
}
#Preview {
SwiftUIView()
}
The first overlay works as expected. The second overlay overlays everything except toolbar buttons. How to fix this issue?
Tried different ways but all of them unsuccessful. Tried .fullScreenCover(...) but it has its own problems with animation, sequential dismissing-presenting and etc.
Example
struct OverlayView: ViewModifier {
@Binding var isPresented: Bool
let modalContent: AnyView
func body(content: Content) -> some View {
ZStack {
content
if isPresented {
modalContent
.edgesIgnoringSafeArea(.all)
}
}
}
}
extension View {
func overlayModal(isPresented: Binding<Bool>, @ViewBuilder modalContent: () -> some View) -> some View {
self.modifier(OverlayView(isPresented: isPresented, modalContent: AnyView(modalContent())))
}
}
struct ContentView: View {
@State var showModal = false
@State var showNext = false
var body: some View {
NavigationStack {
Button {
showModal = true
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
showModal = false
showNext = true
}
} label: {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
NavigationLink {
EmptyView()
} label: {
Rectangle()
.frame(width: 44, height: 44)
}
}
}
.navigationDestination(isPresented: $showNext) {
ContentView()
}
}
.overlayModal(isPresented: $showModal) {
Color.black.opacity(0.5)
}
}
}
NavigationStackinSwiftUIView2, because this view is inside the firstNavigationStack. Anyway, a workaround for the overlay not covering the toolbar buttons might be to hide the toolbar when the overlay is showing. For more detail, please elaborate your example so that it actually shows the toolbar and overlays.NavigationStackfrom the second instance nothing changesContentViewyou still have a nestedNavigationStack. I would suggest, trying to avoid this in your real code.SwiftUIViewandSwiftUIView2seem to be irrelevant and somewhat misleading, since the "second" view is actually the same view and not a different one. I suggest removing them and just keeping the actual example.