简体   繁体   English

强制iOS设备更改方向

[英]Force the iOS device to change orientation

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. 我有一个ViewControllerA在Potrait模式和ViewControllerB在横向模式。 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. 当设备从potrait更改为横向时,我可以看到ViewControllerB在横向模式下打开,但是当设备旋转回potrait模式时, ViewControllerA会显示但仍处于横向模式。 Could anybody help me understand how can I display it back in potrait mode? 有人能帮我理解如何在potrait模式下显示它?

I have tried following things but nothing worked for me: 我试过以下事情,但没有任何对我有用:

Added following code in ViewControllerA in viewWillAppear : viewWillAppear中的ViewControllerA中添加了以下代码:

[[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;
}

Hope this helps you by using supportedInterfaceOrientationsForWindow 希望这可以通过使用supportedInterfaceOrientationsForWindow来帮助您

Appdelegate.h Appdelegate.h

@property () BOOL restrictRotation;

in Appdelegate.m 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 requiredViewController.m

-(void) restrictRotation:(BOOL) restriction
{

  appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
  appDelegate.restrictRotation = restriction;
}

//In ViewdidLoad() //在ViewdidLoad()中

- (void)viewDidLoad
 {
   [super viewDidLoad];
   [self restrictRotation:YES];//Set YES if required in Landscape mode
 }

请试试

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

Use below code 使用以下代码

-(void) viewDidAppear:(BOOL)animated
{
[UIViewController attemptRotationToDeviceOrientation];
[super viewDidAppear:animated];
}

& also in viewWIllAppear write 并且还在viewWIllAppear中写道

[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM