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 } } } }
UIHostingController
//PlaygroundPage.current.liveView = UIHostingController.init(rootView: CounterView())
Setting Live View
PlaygroundPage.current.setLiveView(CounterView())
Leave a Reply