NavigationLink Works Only for Once
[UPDATE] Nov 5, 2020 - pawello2222 says that this issue has been fixed in Xcode 12.1.
[UPDATE] Jun 14, 2020 - Quang Hà says that this issue has come back in Xcode 11.5.
[UPDATE] Feb 12, 2020 - I checked for this issue in Xcode 11.4 beta and found that this issue has been resolved.
I was getting the same issue in my project too, when I was testing it in Xcode's simulator. However, when I launched the app on a real device (iPhone X with iOS 13.3), NavigationLink
was working totally fine. So, it really does seem like Xcode's bug.
Navigation Link only works once in SwiftUI
It works on devices not on simulator.
SwiftUI - Navigation Link - doesn't work second time
This is a simulator bug only, if you have a device to test on the NavigationLink
works correctly multiple times. Hopefully Apple will fix this soon.
NavigationLink in SwiftUI not working anymore
I can see from the comments that you've found out it will only work in a NavigationView
and are now wondering why. It only matters that your view is embedded in a NavigationView
directly above it the View hierarchy. For example, this code would work:
struct FirstView: View {
var body: some View {
NavigationView {
NavigationLink(label: "Go to next view", destination: NextView())
}
}
}
struct NextView: View {
...
While this won't:
struct FirstView: View {
@State var modalPresented = false
var body: some View {
NavigationView {
Button("Show fullscreen cover"){
modalPresented = true
}
}
.fullScreenCover(isPresented: $modalPresented, content: SecondView())
}
}
struct SecondView: View {
var body: some View {
NavigationLink(label: "Go to next view", destination: NextView())
// Doesn't work because the view is in a fullScreenCover and therefore not a part of the NavigationView.
}
}
SwiftUI Navigation does not work as expected with 3 views when navigationLink to third view is embedded in a navigationBar button
The problem is that you have a NavigationLink
outside your NavigationView
.
.navigationBarItems(leading: Button("Back to 1st View") {
self.active = false
}, trailing: NavigationLink( /// this is not inside your NavigationView!
"Show Third View",
destination: ThirdView(thirdViewActive: $thirdViewLink),
isActive: $thirdViewLink
)
This will not work and you'll run into weird issues. NavigationLink
always needs to be inside NavigationView
. You should follow your approach with the "This works as expected but is not ideal for my purposes", and just pass in an EmptyView
to hide it.
struct SecondView: View {
@Binding var active: Bool
@State private var thirdViewLink: Bool = false
var body: some View {
VStack {
Spacer()
// This works as expected but is not ideal for my purposes
NavigationLink(destination: ThirdView(thirdViewActive: $thirdViewLink), isActive: $thirdViewLink) {
EmptyView()
}
Spacer()
}.padding()
.navigationBarTitle("Second View")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button("Back to 1st View") {
self.active = false
}, trailing:
Button("Show Third View") {
self.thirdViewLink = true
}
)
}
}
Related Topics
Stop Uiwebview from "Bouncing" Vertically
Setting Direction for Uiswipegesturerecognizer
Framework Not Found Googletoolboxformac
Foreign Key Relationship Mapping with Restkit
What Is 'Vary for Traits' in Xcode 8
iOS Multiple Columns in Uitableview
Scrollview Gesture Recognizer Eating All Touch Events
Does H.264 Encoded Video with Bt.709 Matrix Include Any Gamma Adjustment
Instruments Allocations Track Alloc and Dealloc of Objects of User Defined Classes
Xcode 9 Swift Language Version (Swift_Version)
How to Disable Back Swipe Gesture in Uinavigationcontroller on iOS 7
Developing for iOS Device in Windows Environment with Flutter
What's the Best Way to Find the User's Documents Directory on an Iphone
Iad Is Shutting Down. Should I Remove the Iad Framework from All My Applications
Xcode 8 - Ib Designables - Failed to Render and Update Auto Layout Status, the Agent Crashed
Invalid Update: Invalid Number of Rows in Section 0
Counting the Number of Lines in a Uitextview, Lines Wrapped by Frame Size