How to create a SwiftUI #Preview in Xcode 15 for a view with a @Binding – Swift

by
Ali Hasan
axios swift-concurrency swiftui-animation xcode15

Quick Fix: To create a SwiftUI preview for a view with a @Binding in Xcode 15, use the following code snippet:

#Preview {
    @State var value = true
    return SpecialButton(isOn: $value)
}

This #Preview macro takes a closure that returns the SwiftUI component to be previewed. You can use it to declare local state or set up any other necessary variables within the closure.

The Solutions:

Solution 1: Using “return” Statement with #Preview Macro

To create a SwiftUI preview for a view with a @Binding in Xcode 15, you can use the #Preview macro along with a “return” statement. Here’s how it’s done:

#Preview {
    @State var value: Bool = true
    return SpecialButton(isOn: $value)
}

In this code, the #Preview macro takes a closure that returns the view to be previewed. Inside the closure, you can declare local variables, such as the @State variable “value” in this case, and set it up as needed. The return statement then specifies the view to be previewed, which is the SpecialButton with the @Binding to the “value” variable.

Solution 3: Using constant binding

One possible solution to preview a SwiftUI view with a @Binding in Xcode 15 is to use a constant binding. This involves creating a constant variable and passing it as the binding value to the view. Here’s an example:

struct SpecialButton_Preview: PreviewProvider {
    static var previews: some View {
        let isOn = Binding(get: { true }, set: { _ in })
        SpecialButton(isOn: isOn)
    }
}

In this example, the isOn variable is a constant binding that always returns true when read and does nothing when written to. This allows the preview to display the view with the button in the on state, without the need for additional state management.

Solution 4: Reusable SwiftUI Preview with `@Binding`

To preview a SwiftUI view with a @Binding in Xcode 15, you can utilize the following reusable view:

public struct PreviewBindingWrapper<T, Content: View>: View {
    @State private var wrappedBinding: T
    private let content: (Binding<T>) -> Content

    public init(wrappedBinding: T, @ViewBuilder content: @escaping (Binding<T>) -> Content) {
        self._wrappedBinding = State(wrappedValue: wrappedBinding)
        self.content = content
    }

    public var body: some View {
        content($wrappedBinding)
    }
}

Usage:

#Preview {
    PreviewBindingWrapper(true) { booleanBinding in
        SpecialButton(isOn: booleanBinding)
    }
}

Solution 5: Using `init()` within the `#Preview` Block

To resolve the ambiguity error with #Preview when using a @Binding property, you need to define the state property within the init() block of the #Preview closure.

#Preview {
    @State var value: Bool = true
    return SpecialButton(isOn: $value)
}

This approach ensures that the state property is correctly initialized and bound to the preview.

Q&A

How can I return the view to preview in SwiftUI #Preview ?

You have to return the view to preview inside the #Preview closure

What should I do if I need a mutable value in the SwiftUI #Preview?

You can use a PreviewWrapper to have a mutable value

What is PreviewBindingWrapper and how can I use it?

PreviewBindingWrapper is a reusable view that allows you to manipulate the @Binding in the Live Preview

Video Explanation:

The following video, titled "How to Make an App - Lesson 1 (2023 / Xcode 14 / SwiftUI) - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Confidently learn how to make an app (no coding experience required). Used in classrooms and Apple stores, these videos are designed for ...