Here is three view,ContentView,ListView and MyView。Click first bottom icon show ListView,then click NavigationLink display MyView.Click the button display sheet,dismiss the sheet the ListView displayed on the screen.
struct ContentView: View {
var body: some View {
NavigationView {
TabView {
ListView().tabItem {
Label("list view", systemImage: "face.smiling")
}
Text("list 2 content").tabItem {
Label("text", systemImage: "face.smiling")
}
}
}
}
}
struct ListView: View {
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
LazyHStack(alignment: .top) {
LazyVStack {
NavigationLink(destination: MyView()) {
Text("test")
}
}
}
}
}
}
struct MyView: View {
@State private var isShowingSheet = false
@Environment(.presentationMode) var presentationMode
var body: some View {
Button(action: {
isShowingSheet.toggle()
}) {
Text("Show Sheet")
}
.sheet(isPresented: $isShowingSheet,
onDismiss: didDismiss)
{
VStack {
Text("License Agreement")
.font(.title)
.padding(50)
Text("""
Terms and conditions go here.
""")
.padding(50)
Button("Dismiss",
action: { isShowingSheet.toggle() })
}
}
}
func didDismiss() {
presentationMode.wrappedValue.dismiss()
}
}
In MyView sheet dismissed,should display MyView
2
Answers
It works for me if you re-organise the hierarchy as suggested in the comments and put the
NavigationView
inside theTabView
, instead of vice versa.After the sheet is dismissed,
MyView
can be seen again. However, it immediately navigates back toListView
due to the.onDismiss
parameter to the.sheet
modifier. So if you don’t want it to navigate back (perhaps for testing), remove the.onDismiss
parameter, or comment out the body ofdidDismiss
:I managed to fix it. First off, you need to use a NavigationStack instead of the soon to be deprecated NavigationView, like this:
I’d suggest you to also keep track of the current selected tab using the selection property in the TabView constructor like I did above.
Then, in MyView, do not use the onDismiss, ratehr use the isShowing sheet State variable to show or hide the sheet this way:
Hope I was able to help you. Let me know if that worked for you!