6

I have a MainView which is a Navigation View and it presents a View through NavigationLink i.e. SubView here. In SubView, I want to present a view on top of the subview on a button toggle while hiding the navigation bar and the subview contents. I tried with ZStack but it doesn't seem to hide the navigation bar as it is still a part of the navigation stack. Is there any other way to achieve this?

The code looks like:

struct MainView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: SubView()) {
                    Text("Go to Another View")
                }
            }
        }
    }
}

struct SubView : View {
    @State private var showTopSheet: Bool = false
    var body: some View {
        ZStack {
            Text("Content goes here")
            if showTopSheet {
                TopSheet(showTopSheet: $showTopSheet).transition(.move(edge: .top))
            }
        }.navigationBarTitle("SubView")
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(trailing: Button(action: {
                self.showTopSheet.toggle()
            }) {
                Image(systemName: "folder.circle")
            })
    }
}

struct TopSheet: View {
    @Binding var showTopSheet: Bool
    var body: some View {
        GeometryReader { geo in
            VStack {
                ZStack {
                    Rectangle().fill(Color.orange)
                        .frame(height: geo.size.height * 364 / 744)
                    HStack {
                        Text("View")
                        Button(action: {
                            self.showTopSheet.toggle()
                        }) {
                            Image("Icon Close").renderingMode(.original)
                        }
                    }
                }
                Spacer()
            }.edgesIgnoringSafeArea([.top, .bottom])
                .background(Color(UIColor(red:0.1, green:0.1, blue:0.12, alpha:0.9)))
        }
    }
}

Thanks in advance :)

user2580
  • 239
  • 5
  • 13
  • 2
    Does those answer your question https://stackoverflow.com/a/63056850/12299030 or https://stackoverflow.com/a/61521113/12299030? – Asperi Aug 14 '20 at 06:54
  • 2
    Thanks @Asperi for the solution. I solved the issue by wrapping the navigation View (Main View) inside a ZStack, optionally presenting the TopSheet view using an observable object. – user2580 Aug 14 '20 at 16:16

0 Answers0