繁体   English   中英

强制iOS设备更改方向

[英]Force the iOS device to change orientation

首先,我想再次提出同样的问题而道歉,这个问题曾多次在本论坛中提出过。 但是,我的问题是我已经尝试了所有建议的解决方案,但我仍然无法解决我的问题。

我有一个ViewControllerA在Potrait模式和ViewControllerB在横向模式。 当设备从potrait更改为横向时,我可以看到ViewControllerB在横向模式下打开,但是当设备旋转回potrait模式时, ViewControllerA会显示但仍处于横向模式。 有人能帮我理解如何在potrait模式下显示它?

我试过以下事情,但没有任何对我有用:

viewWillAppear中的ViewControllerA中添加了以下代码:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

还在此视图控制器中添加了以下功能:

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
     return UIInterfaceOrientationPortrait;
}

希望这可以通过使用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()中

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

请试试

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

使用以下代码

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

并且还在viewWIllAppear中写道

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

暂无
暂无

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

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