I'm just trying to set up my app so that only one view can be viewed in landscape mode.
I've tried just about everything from shouldAutorotate
to supportedInterfaceOrientation
to preferedInterfaceOrientationForPresentation
, and setting [UIDevice currentDevice]
's orientation to portrait. I have Landscape enabled in Info.plist and the project's general section.
Firstly make your application portrait only in your info.plist
Create the following property in your AppDelegate class:
@property BOOL restrictRotation;
Then create the following method in your AppDelegate.m class:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
Create the following method in your ViewController and call it right before you want to permit landscape orientation. (Call it with true first in your viewDidLoad method to make sure rotation is restricted)
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
like this:
[self restrictRotation:NO];
and after you are done with your landscape view and its dismissed, call this immediately:
[self restrictRotation:YES];
Hope this answers your question.
also change orientation after locking it:
-(void)forceOrientation:(UIInterfaceOrientation)orientation{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:orientation]forKey:@"orientation"];
}
For iOS 6 and above to clear the warning in AppDelegate while using Gurtej Singh answer you can replace the AppDelegate code with :
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window API_AVAILABLE(ios(6.0)) API_UNAVAILABLE(tvos)
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With