Playground view adjustment on Mac and iPad

I'm building my playground on my Mac. I've chosen iOS playground and my views are presented well in the mac timeline, but when I run the playground on my iPad, the views don't bound with the iPad simulator.

Is there any way to adjust that or am I doing it wrong? I ask this because the terms don't specify where the judges are going to review the projects, if on macs or iPads or both.

Thanks in advance.

Replies

Same

I have the same issue. It would be great if an Apple employee could respond to this thread!

I had the same problem and I fixed it partially with the following hack:


After setting your view as PlaygroundPage.current.liveView, make an UIView.animate with an dealy and duration of about 0.01. As animation, do something that the user can't see, like setting the alpha from a view from 0.0 to 0.01. Now, when the completion handler is entered, you will get the correct size of the view that you did set as your liveView, simply by calling yourView.frame.size. That's where you can set up your views /scenes correctly.


I know, it's an ugly hack, but it works. Keep in mind, as soon as you resize the running playground in the app, the views will get messed up again. But you only have to restart the playground and it will fit again.

Hope it helps!

Yes I've met the same problem!!!! I took a whole day to find the solution but failed. I print the size of the main screen which is (768,1024) but the view in the time line is small than that!.

And how do you recognize when the view is resized by the user?

I haven't found a solution for that - Sadly /:

I think the best solution at this point is to specify which device (Mac or iPad) your playground functions the best on in the essay.

I would suggest using Auto Layout constraints for all your views. Your liveView may be resized at runtime and constraints will resize your subviews in relation to its size.


Also, you can react to size changes of the live view by:

override var frame: CGRect {
     didSet {
          // React to new frame here.
     }
}


Another option:

In a view controller..

override func viewDidLayoutSubviews() {
     super.viewDidLayoutSubviews()

     // Layout here.
}

In a view

override func layoutSubviews() {
     super.layoutSubviews()

     // Layout here.
}



Also, you can set the frame of your live view yourself when running in Xcode:

let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 500, height: 200)
PlaygroundPage.current.liveView = view


but Swift Playgrounds on iOS does not respect this.