Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 8 playground live view doesn't work

In my Swift playground, I entered the following code to test the live view feature:

let view = UIView()

PlaygroundPage.current.liveView = view

But for some reason the live view doesn't display on the right in the Assistant Editor. Initially I thought that Xcode wasn't finished running the playground. But I waited and waited and it still doesn't show.

Screenshot: enter image description here

Any help?

like image 924
Blip Avatar asked Mar 25 '17 19:03

Blip


4 Answers

I had this same exact issue. I found a temporary solution.

What I noticed is that if I opened more than one Xcode project it would cause this error. So Just completely quit xcode (command Q) and only open the live playground you are trying to work on and it should work.

Make sure you have the following imports and you might want to give it a frame and color just to make sure it is actually working since your view does not have a frame or color. This code works for me.

import UIKit
import PlaygroundSupport

let view = UIView()
view.backgroundColor = .white
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)

PlaygroundPage.current.liveView = view

playground live view

like image 169
maxcodes Avatar answered Oct 20 '22 00:10

maxcodes


For some users who come here, you may need to turn on live view in XCode

how to turn on live view

like image 24
Ryan Pfister Avatar answered Oct 19 '22 22:10

Ryan Pfister


In Xcode 9.0 as well as having:

PlaygroundPage.current.needsIndefiniteExecution = true

You have to manually open the assistant editor then the playground with current focus will show the UIView.

like image 21
ReaddyEddy Avatar answered Oct 19 '22 23:10

ReaddyEddy


I had the same issue as well and the following line of code fixed it for me:

PlaygroundPage.current.needsIndefiniteExecution = true

Setting a live view is supposed to auto-enable it for you, but for me it wasn't doing it. Must be a bug in Xcode.

like image 29
jacob Avatar answered Oct 19 '22 23:10

jacob