I'm trying to conditionally show a custom button on a view only if it being presented modally. This could be done in UIKit, but I'm trying to do this in SwiftUI.
I tried using the environment variable presentationMode
to check if it's being presented, but the flag is true in both cases:
@Environment(\.presentationMode) private var presentationMode
if presentationMode.wrappedValue.isPresented { // true in both cases
...
}
Is there a way for a view to know if it's being presented or if it's pushed?
Extra Context:
I'm trying to create a custom modifier that will automatically add a dismiss button on views that are reused in both scenarios:
struct OverlayDismissButtonModifier: ViewModifier {
@Environment(\.presentationMode) private var presentationMode
func body(content: Content) -> some View {
content
.overlay(
Group {
if presentationMode.wrappedValue.isPresented { // <-- True in both cases :(
Button(action: { presentationMode.wrappedValue.dismiss() }) {
Label(LocalizedStringKey("Close"), systemImage: "xmark")
.labelStyle(IconOnlyLabelStyle())
.foregroundColor(Color(.label))
.padding(8)
.background(
Circle()
.fill(Color(.systemBackground).opacity(0.8))
)
}
.padding([.top, .trailing])
}
},
alignment: .topTrailing
)
}
}
extension View {
func overlayDismissButton() -> some View {
modifier(OverlayDismissButtonModifier())
}
}