Managing Rotation
Some apps support auto-rotation, while others don’t. View controllers support auto-rotation by default. But you may need to do extra work to make sure onscreen elements remain aligned correctly after a rotation.
Controlling rotation
You can control the rotations supported by your app by modifying the shouldAutoRotateTo InterfaceOrientation: method in any view controller. This method works in a slightly lateral way. When a user rotates a device, iOS runs this method to ask your app if an orientation is supported. If your app returns YES, it auto-rotates the screen. If not, it doesn’t.
You can define the supported rotations like this:
return ((interfaceOrientation == <orientation constant>)
|| (interfaceOrientation == <another orientation constant>)
|| <etc…>);
The “||” character means “or.”
For example, to support upright portrait mode and left landscape only, do this:
return ((interfaceOrientation == (UIInterfaceOrientationPortrait)
|| (interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
If you specify a single orientation, such as UIInterfaceOrientationPortrait, the display will be locked to that orientation. If you return (YES) without any conditionals, all rotations are supported.
You don’t need further code. The auto-rotation just works.
Controlling object layout
If you add some objects to a nib in portrait mode and enable auto-rotation, you’ll ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access