Is UIScreen mainScreen.bounds.size becoming orientation-dependent in iOS8?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
With the release of iOS 8, Apple introduced several changes and improvements under the hood of its mobile operating system. One of the notable changes that developers quickly noticed was the behavior of `[UIScreen mainScreen].bounds.size`. Previously considered to be orientation-independent, the bounds of the main screen in iOS 8 changed to become dependent on the device's current orientation. In this article, we'll delve into the technical implications of this change and provide some examples to illustrate how developers can adapt their applications to accommodate this adjustment.
Historical Context
Prior to iOS 8, the `bounds` property of the main screen, obtained via `[UIScreen mainScreen].bounds.size`, consistently returned a size value that was independent of the device's orientation. The bounds always reflected the portrait dimensions of the screen, irrespective of whether the device was held in portrait or landscape mode. Developers relied on additional computations and auxiliary methods like rotation transformations to adjust their application's layout according to the device's orientation.
Change in iOS 8
With iOS 8, Apple made the `bounds` property of the main screen reflect the current orientation by default. This means that:
- When the device is in portrait orientation, `[UIScreen mainScreen].bounds.size` will return the screen's width as 375 points (for a standard iPhone 6).
- When the device is rotated to landscape orientation, the same property will reflect the landscape width of 667 points.
This change aimed to simplify the development process by providing readily accurate values that adapt to the current orientation, reducing the need for manual computation of dimensions corresponding to the device's orientation.
Example
Here's a simple example illustrating the effect of the change in iOS 8:
- In iOS 7, running this code on an iPhone 6 would log `Width: 375, Height: 667`, regardless of the device's actual orientation.
- In iOS 8, if the device is in landscape mode, the output would log `Width: 667, Height: 375`.
- Auto Layout and Size Classes: As an alternative to manual adjustments, Auto Layout and size classes offer a robust way to manage varying screen sizes and orientations without explicit computation.
- Orientation Notification: Subscribe to orientation change notifications to trigger layout updates when needed, especially if using layout frameworks that don't handle orientation out of the box.
Related reading
- Iterating Through a Dictionary in Swift
- Java 7 language features with Android
- java.lang.ClassNotFoundException Didn't find class on path dexpathlist
- java.lang.ClassNotFoundException Didn't find class on path dexpathlist
- java.lang.IllegalStateException Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException Only fullscreen opaque activities can request orientation
- java.lang.IllegalStateException Only fullscreen opaque activities can request orientation
- java.lang.RuntimeException Can't create handler inside thread that has not called Looper.prepare;
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.