Fareed khan
Fareed khan

Reputation: 87

Swiftui nested navigation issue while using navigation link

My swiftui application structure looks like this

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 ?

Upvotes: 0

Views: 788

Answers (1)

ChrisR
ChrisR

Reputation: 12165

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()
        }
    }
}

Upvotes: 0

Related Questions