Search code examples
swiftuiswiftui-navigationlinkswiftui-navigationviewswiftui-tabview

Swiftui nested navigation issue while using navigation link


My swiftui application structure looks like this

  • Navigation View (enclosing the landing view that is a list view )
  • On selection of a List item Navigation link directs to a Tab View with three tabs (default first tab)

When I use a sole standalone navigation link inside tab view screens to direct to another screen programatically, it navigates succesfully to the mentioned destination, but my binding doesn't work to come back to the previous screen.

Parent View

@State var showCameraPreviewView : Bool = false 
ZStack{
 Button("Show camera") {
  showCameraPreviewView = true
}
 NavigationLink(destination: CameraView(showCameraPreviewView: $showCameraPreviewView),isActive: $showCameraPreviewView){
                 EmptyView()
             }
}

Child View

@Binding var showCameraPreviewView 

Button("Assume capture success"){
   self.showCameraPreviewView = false
}

Toggling showCameraPreviewView binding to false in the destination doesn't get me back to the current screen. Looks straight forward, but doesn't work ! anything that I'm doing wrong ?


Solution

  • I can reproduce your issue, quite strange ... seems like the change of showCameraPreviewView is not accepted because the view is still visible. But I found a workaround with dismiss:

    EDIT for iOS 14:

    struct ChildView: View {
        
        @Environment(\.presentationMode) var presentationMode
    
        @Binding var show: Bool
        
        var body: some View {
            Button("Assume capture success"){
                show = false
                presentationMode.wrappedValue.dismiss()
            }
        }
    }