RSS Feed

Tag Archives: orientation

iOS 6 : How to fix rotation for different UIViewController Class

iOS seems awkward for rotation of iPhone/iPad app screen. Sometimes you need to rotate only single view controller class without rotating any other screen. In XCode if you check App Target (support interface orientations) there are four options are given :
1. Portrait 2. Upside down 3. Landscape left 4. Landscape right.

If you select only 1 and 2, you app will work only in portrait mode. Your rotation code will not affect on iPhone screen. Same for Landscape.

Lets consider my app in which i have 2 screens. Both are inherited from UIViewController class. My first screen (ViewController1) should run only in Portrait mode and my second screen (ViewController1) may work in all orientation (i.e portrait and landscape). Follow these step for fix the such case in your app :

1. In your app delegate make sure you are not using [self.window addSubview:RootController.view];
You should use : [self.window setRootViewController:RootController];

2. Inside App Target select all orientation
Orientaitons

3. In your class ViewController1.m write this code : // This class will work only in Portrait.
– (BOOL)shouldAutorotate {

return NO;
}
– (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

4. In your class ViewController2.m write this code : // This class will work in all orientations.
– (BOOL)shouldAutorotate{

return YES;
}
-(NSUInteger)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskAll;
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

if ([self.navigationController.visibleViewController isKindOfClass:[ViewController2 class]])
return (YES);
else
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

Now run the app. It works like charms !!

Advertisements