Reputation: 1188
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.
Any help?
Upvotes: 14
Views: 15403
Reputation: 3286
For some users who come here, you may need to turn on live view in XCode
Upvotes: 0
Reputation: 1212
I was able to preview viewcontroller's view only explicitly leaving controller's view in playground code like this:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
// ...
}
let controller = MyViewController()
PlaygroundPage.current.liveView = controller
controller.view // <- to the far right of this line in editor you'll be able to expand and see result
Upvotes: 2
Reputation: 21
Setting a UIView as a live view only worked for me on iPad playgrounds but not in Xcode.
To make the view appear in Xcode 9 & 10 playground I wrapped it into a view controller and set that to live view instead (just like the default single view playground does):
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view
}
}
PlaygroundPage.current.liveView = MyViewController()
Upvotes: 2
Reputation: 339
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.
Upvotes: 13
Reputation: 3557
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.
Upvotes: 7
Reputation: 544
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
Upvotes: 30