With SwiftUI (Xcode 11.1), I've got some Views set up with 2-way bindings (using @Binding). Two-way updating works great.
However, how can I instantiate the view from the PreviewProvider?
For example:
struct AddProjectView: View {
@Binding public var showModal: Bool
var body: some View {
return VStack {
Text("Add Project View")
Button("Dismiss") {
self.showModal = false
}
}
}
}
I can't do this, because "true" is not a Binding:
struct AddProjectView_Previews: PreviewProvider {
static var previews: some View {
AddProjectView(showModal: true)
}
}
And I can't do this because "Property wrappers are not yet supported on local properties":
struct AddProjectView_Previews: PreviewProvider {
static var previews: some View {
@Binding var show = true
return AddProjectView(showModal: $show)
}
}
How do we do this?
Thanks!!
.constant
is meant exactly for that:
/// Creates a binding with an immutable
value
.
struct AddProjectView: View {
@Binding public var showModal: Bool
var body: some View {
return VStack {
Text("Add Project View")
Button("Dismiss") {
self.showModal = false
}
}
}
}
struct AddProjectView_Previews: PreviewProvider {
static var previews: some View {
AddProjectView(showModal: .constant(true))
}
}