O'Reilly logo

iOS App Development Portable Genius by Richard Wentk

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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…>);

note_2c.eps

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required