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 insta
If you only need a constant value, use .constant(VALUE)
:
struct YourView_Previews: PreviewProvider {
static var previews: some View {
YourView(yourBindingVar: .constant(true))
}
}
If you need a value that can be changed in the live preview, I like to use this helper class:
struct BindingProvider: View {
@State private var state: StateT
private var content: (_ binding: Binding) -> Content
init(_ initialState: StateT, @ViewBuilder content: @escaping (_ binding: Binding) -> Content) {
self.content = content
self._state = State(initialValue: initialState)
}
var body: some View {
self.content($state)
}
}
Use it like so:
struct YourView_Previews: PreviewProvider {
static var previews: some View {
BindingProvider(false) { binding in
YourView(yourBindingVar: binding)
}
}
}
This allows you to test changing the binding in the live preview.