问题
First of all, I would like to apologize for asking the same question again which has been asked in this forum many times. But, my problem is that I've tried all the suggested solutions but still I haven't got a solution to my problem.
I have a ViewControllerA
in Potrait mode and ViewControllerB
in landscape mode. When the device changes from potrait to landscape, i can see ViewControllerB
opening in landscape mode, but when the device is rotated back to potrait mode, ViewControllerA
is displayed but still in landscape mode. Could anybody help me understand how can I display it back in potrait mode?
I have tried following things but nothing worked for me:
Added following code in ViewControllerA
in viewWillAppear
:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
Have also added following functions in this view controller:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
回答1:
Hope this helps you by using 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 where you need
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
}
回答2:
Please try
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
回答3:
Use below code
-(void) viewDidAppear:(BOOL)animated
{
[UIViewController attemptRotationToDeviceOrientation];
[super viewDidAppear:animated];
}
& also in viewWIllAppear write
[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]
来源:https://stackoverflow.com/questions/31158690/force-the-ios-device-to-change-orientation