I have CustomToggle
and I want to test it via Live Preview
. The problem is that tapping on toggle via Live Preview
does nothing, but works well when running Simulator
.
struct CustomToggle: View {
@Binding var isOn: Bool
// ... displaying CustomToggle view
// ... and toggling isON onTapGesture
}
struct CustomToggle_Previews: PreviewProvider {
@State static var isOn = true
static var previews: some View {
CustomToggle(isOn: $isOn)
}
}
You can define a struct for this purpose:
struct StatefulPreviewWrapper<Value, Content: View>: View {
@State var value: Value
var content: (Binding<Value>) -> Content
var body: some View {
content($value)
}
init(_ value: Value, content: @escaping (Binding<Value>) -> Content) {
self._value = State(wrappedValue: value)
self.content = content
}
}
Then use it in your preview like this:
struct CustomToggle_Previews: PreviewProvider {
static var previews: some View {
StatefulPreviewWrapper(false) { CustomToggle(isOn: $0) }
}
}