Eyes, JAPAN Blog > The Solution for Auto Rotation in iOS 6

The Solution for Auto Rotation in iOS 6

will

この記事は1年以上前に書かれたもので、内容が古い可能性がありますのでご注意ください。

In the iOS 6, to implement autorotate, there are some difference compared with prior to iOS 6.

Difference

1. Compared with iOS 5, it uses the UIApplication object and the root view controller to determine whether the new orientation is allowed. If both objects agree that the new orientation is supported, then the user interface is rotated to the new orientation. Otherwise the device orientation is ignored. Developer must set the supported orientation in the Application and also root view controller
2. When a view controller is presented over the root view controller, the system behavior changes in two ways. First, the presented view controller is used instead of the root view controller when determining whether an orientation is supported. Second, the presented view controller can also provide a preferred orientation. If the view controller is presented full screen, the user interface is presented in the preferred orientation.

Support method

override two method: -(NSUInteger)supportedInterfaceOrientations{} and – (BOOL)shouldAutorotate {}

Example

All the orientations are seted to enable in the storyboard. The object “nav”of “MyNavigationController”, which is subclass of UINavigationController is the root controller, in the stack of nav are view controllers: “settingController”, “rootviewController”.
And in the rootController, a modal view controller “showGraphView” can be called.
The supported orientations of “settingController”, “rootviewController” are the same as “nav”, since they are in the container of “nav”. So we only setting the 2 methods


-(BOOL)shouldAutorotate
 {
 return YES;
 }
 -(NSUInteger)supportedInterfaceOrientations
 {
 if([[self.viewControllers lastObject] isKindOfClass:[RootViewController class]])
 {
 [[self.viewControllers lastObject] shouldAutorotateToInterfaceOrientation:[[UIDevice currentDevice] orientation]]; // call the method about rotation in the RootViewController, because in iOS 6,
 //the method about rotation in sub view controller will never be called
 return UIInterfaceOrientationMaskPortrait;
 }
 else
 {
 return UIInterfaceOrientationMaskPortrait;
 }
 }

In the rootviewController, it will show the modal view controller “showGraphView”, I want this view controller to support the auto rotate. So I override the same two methods

-(BOOL)shouldAutorotate
 {
 return [self shouldAutorotateToInterfaceOrientation:[[UIDevice currentDevice] orientation]];
 }
-(NSUInteger)supportedInterfaceOrientations
 {
 return UIInterfaceOrientationMaskAll;
 }
//this method to support prior to iOS 5
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
 // Return YES for supported orientations
 if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) return NO;
//NSLog(@"in ShowGraphView interfaceOrientation = %d", interfaceOrientation);
 if(interfaceOrientation == UIInterfaceOrientationPortrait){
 //[self.navigationController dismissModalViewControllerAnimated:YES];
 [self performSelector:@selector(dismissModal) withObject:nil afterDelay:0.1];
 return YES;
 }
 //return (interfaceOrientation == UIInterfaceOrientationPortrait);
 return YES;
 }

References

1. Official documents from Apple

2. Japanese blog

3. Chinese blog

Comments are closed.