Search code examples
iosswiftxcodeipadswift-playground

Swift Playground UIView has incorrect size


I've been using a Swift playground in XCode and set the size to be at a 4:3 ratio (like an iPad) at 1024 px by 768 px. However, when I acutally run the code on both an iPad and a Mac, the size shows up incorrectly, and the aspect ratio is incorrect as well. The strange thing is, it works just fine when the size is set to 600 px by 600 px. Thank you in advance! The UI View below should look like an iPad not an iPhone.

Incorrect suze/aspect ratio

enter image description here


Solution

  • as @Sweeper said I too think you can't set the root view, however if you're just playing around to see how your UI looks in playground before you can set it to your actual view in view controller. (though you still have to check if the device is iPad or iPhone and then set the width and height accordingly).

    But here's for you to play, instead of single view app, select playground as blank and then set the width and height there. or make you current code to following, hope it helps, Thanks:

    import PlaygroundSupport
    
    let view = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 1024, height: 768)))
    view.backgroundColor = .white
    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = view
    

    enter image description here