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.
NavigationLink freezes when trying to revisit previously clicked NavigationLink in SwiftUI
Had the same issue - try this. I would call this a hack to be removed when the bug in swiftUI is corrected.
struct ListView: View {
@State private var destID = 0
...
var body: some View {
...
NavigationLink(destination: FileView(path: "/\(cell.FileID)", displaysLogin: self.$displaysLogin)
.navigationBarTitle(cell.FileName)
.onDisappear() { self.destID = self.destID + 1 }
){
constructedCell(FileType: cell.FileType, FileName: cell.FileName, FileID: cell.FileID)
}.id(destID)
Essentially it seems that in some circumstances (iOS 13.3 - Simulator?) the NavigationLink is not reset when the destination view is removed from the navigation stack. As a work around we need to regenerate the Navigation Link. This is what changing the id does. This corrected my issue.
However if you have NavigationLinks that are chained, that is a link that leads to another list of links, then this solution will create side effects; the stack returns to the origin at the second attempt to show the last view.
SwiftUI Form and StackNavigationViewStyle run berserk
Update: With Xcode 12 this bug is fixed.
With Xcode 11.3.1 there seems to be a bug in Form
, making a new copy of the view with every orientation change of the device.
Sample code:
import SwiftUI
struct ContentView: View {
var body: some View {
Form{
Text("Text")
}
}
}
Visual debugger at beginning, rotate left and then rotate right:
A Form
automatically adopts the GroupedListStyle
and it seems that it’s actually there the bug is. Because a List
with GroupedListStyle
shows the same behaviour in the visual debugger.
import SwiftUI
struct ContentView: View {
var body: some View {
List{
Text("Text")
}.listStyle(GroupedListStyle())
}
}
Can't select same row twice in SwiftUI
The bug is fixed by Apple in iOS 13.3 beta 4. Keep in mind that iOS 13.3 was in beta at the time you tested it. It was not a bug in iOS 13.2, so this is nothing to worry about anymore.
Update for iOS 13.3 release:
The bug is fixed on physical devices but is still present on emulator.
SwiftUI sheet gets dismissed the first time it is presented
I had the same problem in an app. After a great deal of research, I found that making the variable an observed object fixed the problem in SwiftUI 1, and it seems to be in SwiftUI 2. I do remember that it was an intermittent problem on an actual device, but it always happened in the simulator. I wish I could remember why, maybe when the sheet appears it resets the bound variable?, but this code fixes the problem:
import SwiftUI
import Combine
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailsView()) {
Text("Open Details View")
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct DetailsView: View {
@ObservedObject var sheetIsPresented = SheetIsPresented.shared
var body: some View {
VStack {
Button("Open") {
sheetIsPresented.value.toggle()
}
}.sheet(isPresented: $sheetIsPresented.value, content: {
SheetView()
})
}
}
struct SheetView: View {
var body: some View {
Color.red
}
}
final class SheetIsPresented: NSObject, ObservableObject {
let objectWillChange = PassthroughSubject<Void, Never>()
static let shared = SheetIsPresented()
@Published var value: Bool = false {
willSet {
objectWillChange.send()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Tested on Xcode 12.1, iOS 14.1 in simulator.
Related Topics
Mobile Safari: Inertia Scrolling on Body and Minimal UI Behavior
Cocos2D Sprite Repeat Animation Forever
App Could Not Authenticate with Facebook and Firebase After Conversion to Swift 3 Syntax
iOS - Combine/Concatenate Multiple Audio Files
Xcode 13 - "Button" Title Not Disappearing
Using Shader Modifiers to Animate Texture in Scenekit Leads to Jittery Textures Over Time
How to Go to Specific Native View Controller from React-Native Code
Svg Viewbox Height Issue on iOS Safari
Does "Let _ = ..." (Let Underscore Equal) Have Any Use in Swift
iOS Wkwebview Swift JavaScript Enable
Url Opening Swift App - Open Works - Called Function Does Not Work
iOS Facebook Sdk Error Domain Com.Facebook.Sdk Code 2 and Code 7
Should I Use Realm Objective-C or Realm Swift in My Mixed Objective-C/Swift Project
iOS Add Button to Widget Extension
Initialization of 'Unsafepointer<Int>' Results in a Dangling Pointer