Build a SwiftUI view in Swift Playgrounds

Description: Easily prototype and play around with SwiftUI views when you use them with Swift Playgrounds. We’ll show you how to build a SwiftUI view in a Xcode-compatible playground, and explore tools to help you easily edit and preview your code. For more on Swift Playgrounds, check out our interactive challenge, “Swan's Quest”, and learn to build your own by watching “Create Swift Playgrounds Content for iPad and Mac”.

Creating an Xcode-compatible Playground (from the Swift Playgrounds app)

From main app screen:

  1. tap See All button in the bottom right hand corner of the screen.
  2. scroll to the end of the Starting points section
  3. tap Xcode Playground

Showing a SwiftUI live view

After opening a playground:

  1. import the PlaygroundSupport framework, which lets us customize the behavior of the playground, including showing live views
import PlaygroundSupport
  1. set the playground live view with the your view /
PlaygroundPage.current.setLiveView(WWDCNotesView())
  1. tap the Run my Code button in the bottom right of the screen to show the live view.

Missing anything? Corrections? Contributions are welcome 😃

Related

Written by

Federico Zanetello

Federico Zanetello

Software engineer with a strong passion for well-written code, thought-out composable architectures, automation, tests, and more.