How to Create an iOS Liveview in Xcode 8/Swift 3

Xcode 8 playground live view doesn't work

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

iOS Playground doesn't show UI preview

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

Xcode 8.3.2 playground live view rendering doesn't work

You need to enable the assistant editor to see the results.

View > Assistant Editor > Show Assistant Editor

Also see here:
iOS Playground doesn't show UI preview
Sample Image

Fix live view issues

Try this code!!

var image = UIImage(named: "m.jpg")

let imageView = UIImageView()

imageView.image = image

imageView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
imageView.backgroundColor = .black

PlaygroundPage.current.liveView = imageView

Multiple Views In Xcode Swift Playground

In your playground file, you create your scene view like this:

let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
let myScene = FirstView(fileNamed: "MyPlayground")

if let scene = myScene {
scene.scaleMode = .aspectFill
sceneView.presentScene(scene)
}

PlaygroundSupport.PlaygroundPage.current.liveView = sceneView

And in your First View file, create your FirstView class. Do not forget to put it as public:

import Foundation
import SpriteKit

public class FirstView: SKScene {

private var label : SKLabelNode!

open override func didMove(to view: SKView) {
// Get label node from scene and store it for use later
label = SKLabelNode(text: "first view")
label.position = view.center
addChild(label)
}
}


Related Topics



Leave a reply



Submit