It seems to be a bug. I've managed to whip up a (dirty) workaround:
private enum SetPresentedViewKey: EnvironmentKey {
static var defaultValue: (AnyView?) -> () {
fatalError()
}
}
private extension EnvironmentValues {
var setPresentedView: (AnyView?) -> () {
get {
self[SetPresentedViewKey.self]
} set {
self[SetPresentedViewKey.self] = newValue
}
}
}
/// A replacement for the buggy (as of Xcode 11 b3) `PresentationLink`.
public struct PresentationLink2<Destination: View, Label: View>: View {
public let destination: Destination
public let label: Label
@Environment(\.setPresentedView) private var setPresentedView
@State private var presentedView: AnyView? = nil
public init(destination: Destination, @ViewBuilder _ label: () -> Label) {
self.destination = destination
self.label = label()
}
private struct _Body<Destination: View, Label: View>: View {
@Environment(\.setPresentedView) private var setPresentedView
let destination: Destination
let label: Label
init(destination: Destination, label: Label) {
self.destination = destination
self.label = label
}
var body: some View {
Button(action: present, label: { label })
}
func present() {
setPresentedView(AnyView(destination))
}
}
public var body: some View {
_Body(destination: destination, label: label)
.environment(\.setPresentedView, { self.presentedView = $0 })
.presentation(presentedView.map {
Modal($0, onDismiss: { self.presentedView = nil })
})
}
}
Just copy the code above into your codebase and use PresentationLink2
instead of PresentationLink
.
As noted by @kozlowsqi, PresentationLink
seems to be broken when embedded in a NavigationView
. What's alarming is that it's still broken as of Xcode beta 3.
Edit: I've filed a radar through the new Feedback Assistant app, FB6525020. Please raise your own and reference mine, and hopefully this will be resolved by beta 4.