I have an environment value as follows:
@Environment(\.isPresented) var isPresented: Bool
This environment value is defined as such:
private struct IsPresented: EnvironmentKey {
static let defaultValue: Bool = false
}
extension EnvironmentValues {
var isPresented: Bool {
get { self[IsPresented.self] }
set { self[IsPresented.self] = newValue }
}
}
extension View {
func isPresented(_ isPresented: Bool) -> some View {
environment(\.isPresented, isPresented)
}
}
I want to read this environment value in one of my views to decide whether or not to show a view as a full screen. However, this line of code doesn't work:
.fullScreenCover(isPresented: self.$isPresented) {
// It says there's no such member in my view.
Thus, my question is, how can I convert my environment value to a binding, since .fullScreenCover expects a binding?
If you want it to be a writable value, I think your Environment value should be Binding<Bool>
instead of just Bool
. This is how the system's presentationMode
works, for example.
private struct IsPresented: EnvironmentKey {
static let defaultValue: Binding<Bool> = .constant(false)
}
extension EnvironmentValues {
var isPresented: Binding<Bool> {
get { self[IsPresented.self] }
set { self[IsPresented.self] = newValue }
}
}
extension View {
func isPresented(_ isPresented: Binding<Bool>) -> some View {
environment(\.isPresented, isPresented)
}
}
struct ContentView : View {
@State private var isPresented = false
var body: some View {
ChildView().environment(\.isPresented, $isPresented)
}
}
struct ChildView : View {
@Environment(\.isPresented) var isPresented: Binding<Bool>
var body: some View {
Button("Test") {
isPresented.wrappedValue = true
}
.fullScreenCover(isPresented: isPresented) {
Text("Sheet")
}
}
}
If you really want @Environment(\.isPresented) var isPresented: Bool
as Bool
, then you can create Binding
in place, like
.fullScreenCover(isPresented: Binding(
get: { isPresented },
set: { isPresented = $0 }
)) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With