shouldAutorotate没有调用视图控制器

时间:2015-09-12 05:00:09

标签: ios uiviewcontroller ios9 screen-rotation

我有一个简单的应用程序,包含一个视图控制器。我从Xcode 7 GM单视图应用程序模板开始,但随后删除了主故事板,并设置了我的视图控制器:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let vc = ViewController()

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.rootViewController = vc
    window!.makeKeyAndVisible()

    return true
}

在我的信息plist中,我在支持的界面方向下指定了所有方向,并且应用程序在iPad上旋转到所有方向。

但是,在我的简单视图控制器中,永远不会调用shouldAutorotate()supportedInterfaceOrientations()方法。这是一个问题,因为我正在尝试启用和禁用自动旋转的UI控件。什么可能阻止这些方法被调用?

示例项目here(需要Swift 2)

¹non - UINavigationController

3 个答案:

答案 0 :(得分:5)

根据Apple开发者论坛上的这篇文章,如果您启用了iPad多任务处理(iOS 9中的新增功能),则无法再控制您支持的方向:

https://forums.developer.apple.com/message/13508#13508

你可以添加计数器旋转,但它并不漂亮,至少就我所知。我得到了它的工作,但是当你旋转时无法禁用角落的动画,所以你看到它看起来像是在旋转,但是内容不会旋转,这会让你看起来很怪异。

这是我用来对抗旋转的代码。请注意,我必须隐藏状态栏,否则它也会旋转,我无法弄清楚如何对抗它。

另请注意,在self.navigationController.view.superview.superview上执行自动轮播可能不是最好的方式,并且可能在将来某个时候中断。可能有更好的方法来获得用于对抗旋转的正确视图。显然,如果您不使用导航控制器,则需要传入不同的视图。 YMMV。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.startingInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self updateLayoutsForCurrentOrientation:toInterfaceOrientation view:self.navigationController.view.superview.superview];
}

- (void)updateLayoutsForCurrentOrientation:(UIInterfaceOrientation)toInterfaceOrientation view:(UIView *)view {
    CGAffineTransform transform = CGAffineTransformIdentity;

    if (self.startingInterfaceOrientation == UIInterfaceOrientationPortrait) {
        switch (toInterfaceOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                transform = CGAffineTransformMakeRotation(M_PI/2.0f);
                break;
            case UIInterfaceOrientationLandscapeRight:
                transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
                break;
            case UIInterfaceOrientationPortrait:
                transform = CGAffineTransformIdentity;
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                transform = CGAffineTransformMakeRotation(M_PI);
                break;
            default:
                break;
        }
    }
    else if (self.startingInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        switch (toInterfaceOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
                break;
            case UIInterfaceOrientationLandscapeRight:
                transform = CGAffineTransformMakeRotation(M_PI/2.0f);
                break;
            case UIInterfaceOrientationPortrait:
                transform = CGAffineTransformMakeRotation(M_PI);
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                transform = CGAffineTransformIdentity;
                break;
            default:
                break;
        }
    }
    else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        switch (toInterfaceOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                transform = CGAffineTransformIdentity;
                break;
            case UIInterfaceOrientationLandscapeRight:
                transform = CGAffineTransformMakeRotation(M_PI);
                break;
            case UIInterfaceOrientationPortrait:
                transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                transform = CGAffineTransformMakeRotation(M_PI/2.0f);
                break;
            default:
                break;
        }
    }
    else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        switch (toInterfaceOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                transform = CGAffineTransformMakeRotation(M_PI);
                break;
            case UIInterfaceOrientationLandscapeRight:
                transform = CGAffineTransformIdentity;
                break;
            case UIInterfaceOrientationPortrait:
                transform = CGAffineTransformMakeRotation(M_PI/2.0f);
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
                break;
            default:
                break;
        }
    }
    view.transform = transform;
}

这些代码大部分都是根据Jared Sinclair的JTSImageViewController(经过他的许可发布)改编而来,可以在github上的MIT许可下获得:https://github.com/jaredsinclair/JTSImageViewController

答案 1 :(得分:2)

要选择不参与Slide Over和Split View,请将UIRequiresFullScreen键添加到Xcode项目的Info.plist文件中,并应用布尔值YES。

答案 2 :(得分:0)

我刚试过这个帖子中的解决方案: https://stackoverflow.com/a/32782517/1679627

请注意,info.plist中有两个条目,一个用于iPhone上支持的方向,另一个用于iPad。