5

I can't figure out how to create and display a live view in an Xcode 8/Swift 3 Playground. If Apple has comprehensive documentation on playgrounds and live views, I can't find it, and all my searching online reveals either tutorials on Xcode 7 or the new iPad playgrounds. The few examples I have found set up Mac OS views, which is not what I want (I work mostly in iOS, so the code I want to test out is iOS code, dealing with iOS view hierarchies.)

Can somebody give me the bare minimum setup to create a live view and install views into it from an Xcode 8 playground?

Then, can somebody point me to some good documentation on playgrounds, and how you configure and use them?

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • see: http://stackoverflow.com/a/33543820/1457385 – shallowThought Nov 06 '16 at 13:46
  • @shallowThought This is not for Xcode 8. – dasdom Nov 06 '16 at 13:47
  • Sorry. Added answer. – shallowThought Nov 06 '16 at 13:52
  • 1
    I've updated the accepted answer in the [dupe candidate Q&A](http://stackoverflow.com/questions/33543460/) to include the minor difference between pre and post Xcode8. It would probably be valuable to link these two threads by dupe marking this one to the [dupe candidate Q&A](http://stackoverflow.com/questions/33543460/), especially as this question has now been answered. @DuncanC as you have the Swift gold badge, if you feel it appropriate, use your dupe-hammer to possible make this connection. – dfrib Nov 06 '16 at 14:14

1 Answers1

5

It works like this:

import UIKit
import PlaygroundSupport

let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
view.backgroundColor = UIColor.red

PlaygroundPage.current.liveView = view

Unfortunately I'm not aware of a good documentation. But I believe there is a book by Erica Sadun about Playgrounds.

dasdom
  • 13,975
  • 2
  • 47
  • 58