Johnd
Johnd

Reputation: 624

How to change the size of the Xcode Playground Simulator when using live view?

I created a Swift Playground on my iPad and did all of my graphics positioning on the iPad simulator. Does anyone know how to edit the size of the Xcode playground simulator? Thanks in advance.

Upvotes: 16

Views: 8806

Answers (4)

MGY
MGY

Reputation: 8463

SwiftUI Solution

if you're using the SwiftUI on Xcode Playground Simulator when using live view... You can basically set the frame size before setting the Live View.

struct ContentView: View {
    var body: some View {
        VStack {
            Spacer()
            Text("Test")
            Spacer()
        }
        .frame(width: 700, height: 700, alignment: .center)
    }
}

PlaygroundPage.current.setLiveView(ContentView())

That's it!

Upvotes: 2

Roman Aliyev
Roman Aliyev

Reputation: 273

import PlaygroundSupport
import UIKit

let viewController = UIViewController()
viewController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 568)
PlaygroundPage.current.liveView = viewController

Seems to me this is the only working example at the moment (Xcode 12.5). Setting the view.frame is the key here.

Upvotes: 18

Adam Smaka
Adam Smaka

Reputation: 6393

let vc = ViewController()
vc.view.frame.size = CGSize(width: 375, height: 667)
PlaygroundPage.current.liveView = vc.view

Upvotes: 5

C. Lee
C. Lee

Reputation: 3287

To change the size of a playground's liveView, set the preferredContentSize of the viewController.

import PlaygroundSupport
import UIKit

let viewController = UIViewController()
viewController.preferredContentSize = CGSize(width: 600, height: 600)
PlaygroundPage.current.liveView = viewController

Upvotes: 18

Related Questions