I saw this stackoverflow post:shouldAutorotateToInterfaceOrientation is not working in iOS 6. The answer was to add a category called RotationIn_IOS6. I have done that and the views for the different viewcontrollers are working correctly in iOS6. The problem is in iOS5.
I only need a few views to rotate in all directions, the rest should be in portrait or either PortraitUpsideDown. The problem i face is that either all don't rotate(code1) or after it rotates to landscape, it remains in the same orientation until you rotate back to portrait(code2).
Code1:
@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
@end
Code 2:
@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if([self.visibleViewController isKindOfClass:[MyClassToRotate class]])
{
return [self.visibleViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
Need some guidance on this. Not sure how to solve this.
EDIT:
Code3:
1) Removed the shouldAutorotateToInterfaceOrientation from the Category. 2) Added this code to my particular class
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//from here you Should try to Preferred orientation for ViewController
return TRUE;
}
Result -> All the viewcontroller locked in portrait. Cannot access PortraitUpsideDown. Once in my viewcontroller, it can rotate, but when you get out of the viewcontroller, locked in landscape..
EDIT 2:
Each viewcontroller contains these code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}