强制iOS设备更改方向

强制iOS设备更改方向

问题描述:

首先,对于再次在这个论坛中多次提出的相同问题,我深表歉意.但是,我的问题是我尝试了所有建议的解决方案,但仍然没有解决我的问题的方法.

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.

我在Potrait模式下有一个ViewControllerA,在风景模式下有一个ViewControllerB.当设备从potrait变为风景时,我可以看到ViewControllerB在风景模式下打开,但是当设备旋转回到potrait模式时,会显示ViewControllerA,但仍处于风景模式.有人可以帮助我了解如何将其显示为potrait模式吗?

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:

viewWillAppearViewControllerA中添加了以下代码:

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

希望通过使用supportedInterfaceOrientationsForWindow

Hope this helps you by using supportedInterfaceOrientationsForWindow

Appdelegate.h

@property () BOOL restrictRotation;

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

//在需要的地方拨打此电话

requiredViewController.m

-(void) restrictRotation:(BOOL) restriction
{

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

//在ViewdidLoad()

//In ViewdidLoad()

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