mercredi 3 octobre 2012

Orientation since iOS 6

Handle orientation in iOS applications was a well known operation prior to iOS 6.
Now the method shouldAutorotateToInterfaceOrientation: is deprecated, and few new methods appeared. How does it work now ?


First thing, the values associated with the UIInterfaceOrientations key of your app's Info.plist should now reflect all the orientations in which your application could be.

Then, shouldAutorotateToInterfaceOrientation is now splited in two methods:
- (BOOL)shouldAutorotate;
As its name indicates, this method is aimed to return whether the viewController should auto rotate or not.

Second method is:
- (NSUInteger)supportedInterfaceOrientations;
In this method you have to return a mask of the viewController's supported interface orientations.
Warning: the returned supported interface orientations must be included in the orientations supported by the application (in the Info.plist).

Those two methods are only called on the rootViewController of the window. Feel free to forward those messages.

A third method appeared on UIViewController:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
You may override this method for a presented UIViewController if you want it to rotate to a specific orientation, otherwise it will be presented in the current interface orientation.


Easy isn't it ?

There's just one trick:
shouldAutorotate and supportedInterfaceOrientations are only called on the rootViewController, and that's normal, but UINavigationController does not forward those messages ! So if you're using a UIViewController, i recommend you to use a subclass in which you override those methods.
Here is an example:

@interface MyNavigationController : UINavigationController

@end

@implementation MyViewController

- (BOOL)shouldAutorotate
{
    return [[self topViewController] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}

@end

Aucun commentaire:

Enregistrer un commentaire