How to lock device orientation in iOS 7 and iOS 8
I have a problem with my application. I am unable to lock the orientation of my application. All I have to do is lock one view controller in landscape mode and the rest in portrait.
This is the hierarchy of my application.
*Navigation Controller *TabBarController *ViewControllers
You need to return NO
from shouldAutorotate
and the orientation of the landscape from supportedInterfaceOrientation
what you want to be in the landscape.
On the other hand, return NO
too from the method layout shouldAutorotate
and portrait from supportedInterfaceOrientation
.
In all viewControllers:
-(BOOL)shouldAutorotate {
return NO;
}
What you want in a landscape:
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
In the controllers you want in the portrait:
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
Use below 2 of these methods to lock device orientation to landscape.
- (NSUInteger)supportedInterfaceOrientations{
[super supportedInterfaceOrientations];
return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
{
return YES;
}
// Return YES for supported orientations
return NO;
}
With NavigationController
-(BOOL)shouldAutorotate
-(NSUInteger)supportedInterfaceOrientations
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
this method is never called if you use show segue (push).
change segue 'showDetail' instead of 'show'