iOS Playground doesn't show UI preview

Lubbo picture Lubbo · Nov 5, 2015 · Viewed 41.2k times · Source

I've created a simple playground with XCode 7.1 and I've typed this simple code:

import UIKit 
import XCPlayground 

var str = "Hello, playground" 

let color = UIColor (red: 1 , green: 1 , blue: 0 , alpha: 0 ) 

let view = UIView() 
view.backgroundColor = UIColo (colorLiteralRed: 1 , green: 0 , blue: 0 , alpha: 0 ) 

view.frame = CGRect (x: 0 ,y: 0 ,width: 100 ,height: 100 ) 

let label = UILabel (frame: CGRect (x: 5 , y: 5 , width: 50 , height: 20 )) 

label.text = str 

view.addSubview(label) 

When the playground runs, it doesn't show UIKit object preview, but only debug information:

playground

What am I doing wrong?

Answer

duan picture duan · Nov 5, 2015

Open the preview panel: View > Assistant Editor > Show Assistant Editor

Then in your code:

import PlaygroundSupport
PlaygroundPage.current.liveView = view

Don't forget to give your view a visible frame.


Ps: after Xcode 9, you can create a playground with default view

playground