Skip to content

Instantly share code, notes, and snippets.

@mtgto
Created August 7, 2021 07:28
Show Gist options
  • Save mtgto/5c48c8f319e3a1f992b3be819a74fc61 to your computer and use it in GitHub Desktop.
Save mtgto/5c48c8f319e3a1f992b3be819a74fc61 to your computer and use it in GitHub Desktop.
isActive of NavigationLink is not work when using List with empty container. (iOS 14.5, Xcode 12.5.1, Swift 5.4)
import SwiftUI
// isActive of NavigationLink is not work when using List with empty container.
struct ContentView: View {
@State var isSubviewDisplayed: Bool = false
@State var array: [String] = []
var body: some View {
NavigationView {
List(array, id: \.self) { _ in
NavigationLink(
destination: SubView(isDisplayed: $isSubviewDisplayed),
isActive: $isSubviewDisplayed,
label: {
Text("Push")
})
}
.navigationBarItems(
trailing: NavigationLink(
destination: SubView(isDisplayed: self.$isSubviewDisplayed),
isActive: self.$isSubviewDisplayed
) {
Text("Push")
})
}
}
}
struct SubView: View {
@Binding var isDisplayed: Bool
var body: some View {
// If `array` is empty, isDisplayed changes to false, but SubView does not be popped.
Button("Pop (isDisplayed = \(isDisplayed ? "true" : "false"))") {
isDisplayed = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment