Lubbo Lubbo - 1 month ago 26
iOS Question

iOS Playground doesn't show UI preview

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 I'm doing wrong?

Answer

Use:

XCPlaygroundPage.currentPage.liveView = view

Then you could preview your view in the assistant editor View > Assistant Editor > Show Assistant Editor:

enter image description here


XCode 8

For XCode 8, the above needs to be slightly modified:

  1. Replacing import XCPlayground with:

     import PlaygroundSupport
    
  2. Replacing XCPlaygroundPage.current.liveView = view with:

     PlaygroundPage.current.liveView = view