Created
August 7, 2021 07:28
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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