How to preview SwiftUI views in Xcode Playground.

Previewing SwiftUi code using Xcode playgrounds can be done by using settings live view or UIHostingController.

Let us see this by using the following code snippet (SwiftUI View) which displays a button that increments a counter.

import SwiftUI
import PlaygroundSupport

struct CounterView: View {
    @State private var count: Int = 0

    var body: some View {
        VStack {
            Text("Count: \(count)")
            Button("Increment") {
                count += 1


//PlaygroundPage.current.liveView = UIHostingController.init(rootView: CounterView())

Setting Live View



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.