XYZ
XYZ

Reputation: 27437

How to use SwiftUI inside Xcode Playground?

How to render a SwiftUI view inside Xcode Playground? PreviewProvider does not seem to work with Swift Playground.

struct FooView: View {
    var body: some View {
        Text("Foo")
    }
}

struct SampleView: PreviewProvider {
    static var previews: some View {
        FooView()
    }
}

Upvotes: 3

Views: 1113

Answers (1)

XYZ
XYZ

Reputation: 27437

PlaygroundPage from PlaygroundSupport is what needed to render a SwiftUI view.

import SwiftUI
import PlaygroundSupport

struct FooView: View {
    var body: some View {
        Text("Foo")
    }
}

PlaygroundPage.current.setLiveView(FooView())

enter image description here

and as @cbjeukendrup mentioned this also works with Playgrounds on iPadOS.

enter image description here

Upvotes: 10

Related Questions