UIScreen bounds in iOS 8

A quick reminder of a change in the way iOS 8 reports the bounds of UIScreen. From the UIScreen class reference (my emphasis):

Prior to iOS 8, a screen’s bounds rectangle always reflected the screen dimensions relative to a portrait-up orientation. Rotating the device to a landscape or upside-down orientation did not change the bounds. In iOS 8 and later, a screen’s bounds property takes the interface orientation of the screen into account.

This is likely to be a problem only if you are assuming the screen or window bounds reflect a device in portrait orientation (as was the case in iOS 7). You can see the difference with the code snippet below:

CGRect bounds = [UIScreen mainScreen].bounds;
NSLog(@"(x:%f, y:%f) (w:%f, h:%f)",
      bounds.origin.x,
      bounds.origin.y,
      bounds.size.width,
      bounds.size.height);

On an iPhone 6 running iOS 8 first in portrait and then in landscape orientation we get the output:

(x:0.000000, y:0.000000) (w:375.000000, h:667.000000) // portrait
(x:0.000000, y:0.000000) (w:667.000000, h:375.000000) // landscape

For comparison, on a (simulated) iPhone 5 running iOS 7.1 the output is always the same regardless of orientation:

(x:0.000000, y:0.000000) (w:320.000000, h:480.000000) // portrait
(x:0.000000, y:0.000000) (w:320.000000, h:480.000000) // landscape

Getting the bounds for a fixed coordinate space

If you need to always get the screen bounds of the device as if it was in portrait mode you can use the object returned by the fixedCoordinateSpace property:

CGRect fixedBounds = [UIScreen mainScreen].fixedCoordinateSpace.bounds;

// fixedBounds - same result for portrait and landscape
(x:0.000000, y:0.000000) (w:375.000000, h:667.000000)

Never miss a post

iOS Size Classes Cheat Sheet

Subscribe and also get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time. See Privacy Policy
No time to watch WWDC videos?

Sign up to get my iOS posts and news direct to your inbox and also get my free iOS Size Classes Cheat Sheet

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time. See Privacy Policy
Archives Categories