iOS Playground doesn't show UI preview iOS Playground doesn't show UI preview ios ios

iOS Playground doesn't show UI preview


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

Then in your code:

import PlaygroundSupportPlaygroundPage.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


The accepted answer is a screen-shot, not in-line code, and it shows how to do this pre Xcode 8, so I don't think it's a good answer.

Here is an updated answer For Xcode 8:

There are several steps:

You need to import PlaygroundSupport

import PlaygroundSupport

You need to set PlaygroundPage.current.needsIndefiniteExecution to true:

PlaygroundPage.current.needsIndefiniteExecution = true

You need to add the view hierarchy you want to display to the liveView:

PlaygroundPage.current.liveView = container

The whole playground might look like this:

import UIKitimport PlaygroundSupportlet container = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))view.backgroundColor = UIColor.redcontainer.addSubview(view)PlaygroundPage.current.liveView = containerPlaygroundPage.current.needsIndefiniteExecution = true

Finally, you need to see the live view you just created. On my copy of Xcode 8 (release version) If I try to select view menu>assistant editor>Show Assistant Editor Xcode crashes, every time.

I have to hover my mouse on the right side of the screen in the box to the right of the code, on the line where I create the container view. I see a little eye icon, and if I click on that, it shows the live view without crashing.


For those of you who experience the problem that the assistant editor doesn't do anything (for me it was simply blank), here is how I solved the problem.

  1. Use that code to test: https://stackoverflow.com/a/33543820/4572536 (accepted answer from above)

  2. View -> Assistent Editor -> Assistent Editors on Bottom (or any other option)

The latter will trigger Xcode to redraw the view.

Note: I'm sharing this as a possible workaround, because for me my Xcode didn't draw the view even after full system restarts. I have no idea how to reproduce that issue, but if you happen to bump into it you can try this workaround.