Force iOS device to change orientation
First of all, I would like to apologize for asking the same question again that has been asked on this forum many times. But, my problem is that I tried all the proposed solutions, but still I have no solution to my problem.
I have both ViewControllerA
in Potrait mode and ViewControllerB
in landscape mode. When the device changes from cover to landscape, I see an ViewControllerB
opening in landscape mode, but when the device is rotated back to drive mode, it is displayed ViewControllerA
but still in landscape mode. Can anyone help me figure out how I can display it back in potrait mode?
I tried the following things, but nothing worked for me:
Added the following code to ViewControllerA
in viewWillAppear
:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
Also added the following functionality in this view controller:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
source to share
Hope this helps you with supportedInterfaceOrientationsForWindow
Appdelegate.h
@property () BOOL restrictRotation;
in Appdelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
//restrictRotation is BOOL value
if(self.restrictRotation)//set the bool value to view controller which you want to load in landscape
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskAll;
}
// call this wherever you want
requiredViewController.m
-(void) restrictRotation:(BOOL) restriction
{
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
// In ViewdidLoad ()
- (void)viewDidLoad
{
[super viewDidLoad];
[self restrictRotation:YES];//Set YES if required in Landscape mode
}
source to share
Use below code
-(void) viewDidAppear:(BOOL)animated
{
[UIViewController attemptRotationToDeviceOrientation];
[super viewDidAppear:animated];
}
& also in viewWIllAppear write
[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]
source to share