Scenario:
I have a screen with an NavigationStack
, after going to the second screen, The back button of the NavigationStack
appears. In that screen, by clicking the button "Toggle View"
, a fake modal appears with an opacity animation. Everything ok until now, but the problem is that the back button of the navigationStack is not hiddden by the background of the modal.
How can I fix this?
import SwiftUI
struct ContentView: View {
@State private var isViewAVisible = false
var body: some View {
NavigationStack {
VStack {
NavigationLink("Show Detail View") {
SecondView()
}
}
.navigationTitle("Navigation")
}
}
}
struct SecondView: View {
@State private var isViewAVisible = false
var body: some View {
NavigationStack {
ZStack {
Color.red.ignoresSafeArea()
VStack {
Spacer()
Button("Toggle View") {
isViewAVisible.toggle()
}
}
//logic to present the modal
ZStack {
if isViewAVisible {
Color.black.opacity(0.6).ignoresSafeArea()
VStack {
Button {
isViewAVisible.toggle()
} label: {
Text("Hello")
}
.frame(height: 300)
Button {} label: {
Text("Hello 2 ")
}
.frame(height: 300)
}
}
}
.animation(.easeIn, value: isViewAVisible)
}
}
}
}
The screenshot of how it looks like:
It should like like this:
This solution is using fullScreenCover
modifier and that fix the issue, but now I cannot present the modal with an opacity
animation.
Also if someone has idea on how to do it this way would be great!
Here the code for that view:
'''
struct SecondView: View {
@State private var isViewAVisible = false
var body: some View {
NavigationStack {
ZStack {
Color.red.ignoresSafeArea()
VStack {
Spacer()
Button("Toggle View") {
isViewAVisible.toggle()
}
}
}
.fullScreenCover(isPresented: $isViewAVisible) {
ZStack{
VStack {
Button {
isViewAVisible.toggle()
} label: {
Text("Hello")
}
.frame(height: 300)
Button {} label: {
Text("Hello 2 ")
}
.frame(height: 300)
}
}
.presentationBackground(Color.black.opacity(0.6))
}
}
}
}
'''