skip to Main Content

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


  1. It works for me if you re-organise the hierarchy as suggested in the comments and put the NavigationView inside the TabView, instead of vice versa.

    After the sheet is dismissed, MyView can be seen again. However, it immediately navigates back to ListView 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 of didDismiss:

    struct ContentView: View {
        var body: some View {
            TabView {
                NavigationView {
                    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 {
            VStack {
                Text("ListView").font(.largeTitle)
                ScrollView(.vertical, showsIndicators: false) {
                    LazyHStack(alignment: .top) {
                        LazyVStack {
                            NavigationLink(destination: MyView()) {
                                Text("Go to MyView")
                            }
                        }
                    }
                }
            }
        }
    }
    
    struct MyView: View {
        @State private var isShowingSheet = false
        @Environment(.presentationMode) var presentationMode
        var body: some View {
            VStack {
                Text("MyView").font(.largeTitle)
                Spacer()
                Button("Show Sheet") { isShowingSheet.toggle() }
                Spacer()
            }
            .sheet(isPresented: $isShowingSheet, onDismiss: didDismiss) {
                VStack {
                    Text("License Agreement")
                        .font(.title)
                        .padding(50)
                    Text("""
                    Terms and conditions go here.
                """)
                    .padding(50)
                    Button("Dismiss") { isShowingSheet.toggle() }
                }
            }
        }
    
        func didDismiss() {
            // Don't navigate back
            // presentationMode.wrappedValue.dismiss()
        }
    }
    
    Login or Signup to reply.
  2. I managed to fix it. First off, you need to use a NavigationStack instead of the soon to be deprecated NavigationView, like this:

    struct ContentView: View {
        
        @State var selection: Int = 1
        
        var body: some View {
            NavigationStack {
                TabView(selection: $selection) {
                    ListView()
                        .tabItem {
                            Label("list view", systemImage: "face.smiling")
                        }
                        .tag(1)
                    Text("list 2 content").tabItem {
                        Label("text", systemImage: "face.smiling")
                    }
                     .tag(2)
                }
            }
        }
    }
    

    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:

    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: {
                isShowingSheet = false
            })
            {
                VStack {
                    Text("License Agreement")
                        .font(.title)
                        .padding(50)
                    Text("""
                        Terms and conditions go here.
                    """)
                    .padding(50)
                    Button("Dismiss", action: {
                        isShowingSheet = false
                    })
                }
            }
        }
        
        func didDismiss() {
            presentationMode.wrappedValue.dismiss()
        }
    }
    

    Hope I was able to help you. Let me know if that worked for you!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search