4

I updated my game that I thought would support iPhone Xs and iPad 11inch which it fits the screen perfectly on the simulator (extremely frustrating), but not when testing on the physical devices. It seems the rootView (my SKView) is constrained to the safe area insets. The game scene is constrained just like this image.

enter image description here

I used this method inside the UIViewController class to make the root view as an SKView with the size of the device's screen.

override open func loadView() {

    view = SKView(frame: UIScreen.main.bounds)

}

Then create a scene of the same aspect ratio (but not same size) as the SKView and let it scale to fit inside the SKView. For example, the someSpecifiedSize will be (812,375) on the iPhone X/Xs/XsMax/Xr, (667,375) for iPhones 5,6,7,8 and (667,500) for all iPads except the 11inch. It may be odd, but because of the nature of my game it must be like this.

let scene = SKScene(size: someSpecifiedSize)
scene.scaleMode = .aspectFill

I don't understand why the view incorporates the safe area because I didn't use the insets when defining its size. How do I make the SKView be the size of the device's screen and not the size of the safeArea?

KissTheCoder
  • 679
  • 6
  • 16
  • Have you tried to use the property bounds of the view that is under the controller? – Vanya Nov 17 '18 at 04:29
  • Not sure what you mean. Use them how or on what? – KissTheCoder Nov 17 '18 at 16:37
  • Did you happen to turn off the safe area in the storyboard? – Knight0fDragon Nov 20 '18 at 19:20
  • @Knight0fDragon I don't use storyboard. I create the view and scenes programmatically. Is there an option to turn it off without using storyboard? – KissTheCoder Nov 20 '18 at 22:25
  • Are you sure you are not even using the default storyboard? BTW I highly recommend using the storyboard, less clutter. – Knight0fDragon Nov 20 '18 at 22:26
  • @Knight0fDragon Positive, I deleted the Storyboard file and removed the reference to it under the "Main Interface" field in the target's general tab. I've heard bad things from storyboard so I prefer to avoid using it. – KissTheCoder Nov 20 '18 at 22:32
  • Well whomever told you bad things clearly has no understanding on how to use it lol. Well doing it without the storyboard means you need to disable auto layout constraints that are being made with `translatesAutoresizingMaskIntoConstraints = false` and deal with the safe area inlets in code – Knight0fDragon Nov 20 '18 at 22:43

1 Answers1

1

This code actually works as you wish:

self.view = SKView(frame: view.bounds)
    let scene = SKScene(size: view.bounds.size)
    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .resizeFill
    scene.backgroundColor = .green
    skView.presentScene(scene)

Your problem seems to be variable "someSpecifiedSize" which I cannot tell what actually is, but I guess it respects the safe area.

Vanya
  • 4,973
  • 5
  • 32
  • 57
  • I would like to stay away from the resizeFill scaleMode if I can as it makes the scene too big on iPad causing the physics to behave differently than the iPhone version. I updated the question to clarify "someSpecifiedSize" – KissTheCoder Nov 17 '18 at 16:37
  • @Muffinman2497 when you change scaleMode to aspectFill with this code you can get the full screen as you want without the safe are, so the result is the same with iPhone X family – Vanya Nov 17 '18 at 22:50
  • except resize fill messes with the scenes dimensions, meaning on every device you are offering a different experience – Knight0fDragon Nov 20 '18 at 19:18
  • also, this is breaking constraints, and screws up the storyboard, this is going to get a downvote from me until it is clarified a lot better. – Knight0fDragon Nov 20 '18 at 19:20
  • @Knight0fDragon not sure, but it seems that StoryBoard is not involved in this case and it works with .aspectFill as well – Vanya Nov 21 '18 at 09:33
  • @Knight0fDragon this question is about making view visible even in the safe area parts, not about storyboard, constrains, neither rules of the game. – Vanya Nov 25 '18 at 10:55