如果在iOS中不允许应用程序旋转,如何启用特定视图控制器自动旋转?

时间:2017-01-03 06:19:50

标签: ios iphone

"肖像"是app的info.plist中唯一支持的界面方向,我无法更改它;

但我需要自动旋转特定的视图控制器;

我找不到一种方法可以轻松地做到这一点,我能找到的唯一方法是在接收设备旋转通知时旋转每个视图,文本并重新制作所有约束,但我真的不想这样做。

有没有更简单的方法来实现这个?

3 个答案:

答案 0 :(得分:1)

重点:

  • If you want Rotation in Single OR more Screens so you have to enable it for throughout the application 即可。
  • After enabling it from Target -> General -> Device Orientation you have to manage on specific view controller in which you want rotations.

为此,您必须执行以下操作:

检查应用程序中的所有Device Rotation掩码。 Target -> General -> Device Orientation.

SubClassNavigation Controller并在下面写下代码:

- (BOOL)shouldAutorotate{
    //NSLog(@"%d",[[[self viewControllers] lastObject] shouldAutorotate]);

    return [[[self viewControllers] lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations{
   // NSLog(@"%lu",(unsigned long)[[[self viewControllers] lastObject] supportedInterfaceOrientations]);
    return  [[[self viewControllers] lastObject] supportedInterfaceOrientations];
}

在要旋转的所有视图控制器中返回YES,在不想要旋转的情况下返回No.

    -(BOOL)shouldAutorotate
{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return  UIInterfaceOrientationPortrait;
}

答案 1 :(得分:1)

AppDelegate.m来自要启用旋转的特定视图控制器的调用中尝试此方法。

self.fullScreenVideoIsPlaying为该特定视图控制器设置了YES以进行旋转启用

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.fullScreenVideoIsPlaying == YES){
        return UIInterfaceOrientationMaskLandscapeLeft;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
 }

答案 2 :(得分:0)

在要旋转的控制器中使用此方法

- (NSUInteger)supportedInterfaceOrientations {
 return UIInterfaceOrientationMaskAll;
}