未调用shouldAutorotate

未调用shouldAutorotate

本文介绍了应该在视图控制器上未调用shouldAutorotate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单个视图控制器的简单应用程序.我从Xcode 7 GM Single View Application模板开始,但随后删除了主要的故事板,并按如下所示设置了我的视图控制器:

I have a simple app consisting of a single view controller. I started with the Xcode 7 GM Single View Application template, but then deleted the main storyboard, and set up my view controller like this:

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
}

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

In my info plist, I have all orientations specified under Supported Interface Orientations, and the app rotates to all orientations on iPad.

但是,在我的简单¹视图控制器中,从未调用shouldAutorotate()supportedInterfaceOrientations()方法.这是一个问题,因为我正在尝试启用和禁用自动旋转的UI控件.是什么原因阻止了这些方法的调用?

However, in my simple¹ view controller, the shouldAutorotate() and supportedInterfaceOrientations() methods are never called. This is a problem, because I'm experimenting with a UI control enabling and disabling auto rotation. What could be preventing these methods from being called?

在项目此处(需要Swift 2)

Sample project here (requires Swift 2)

¹non-UINavigationController

推荐答案

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

According to this post on the Apple Developer forum, if you have iPad multitasking enabled (new in iOS 9), you can no longer control the orientations you support:

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

您可以添加一个反向旋转,但这并不是很漂亮,至少就我所知.我可以使用它,但是在旋转时无法禁用角落的动画,因此出现了看起来很奇怪的情况,看起来好像在旋转,但是内容没有旋转.

You can add a counter rotation, but it's not pretty, at least as far as I can tell. I got it working, but was unable to disable the animation of the corners when you rotate so you get this weird looking situation where it looks like it's rotating, but the content doesn't rotate.

这是我用来抵消旋转的代码.请注意,我也必须隐藏状态栏,否则状态栏也会旋转,因此我不知道该如何应对.

Here is the code I was using to counter the rotation. Note that I had to hide the status bar too or it would rotate as well and I couldn't figure out how to counter that.

还请注意,在self.navigationController.view.superview.superview上执行自动旋转可能不是最佳方法,并且将来可能会中断.可能有更好的方法来获取用于抵消旋转的正确视图.显然,如果您不使用导航控制器,则需要传递其他视图. YMMV.

Also note that doing the auto rotation on self.navigationController.view.superview.superview is likely not the best way and may break at some point in the future. There is likely a better way to get the correct view used to counter the rotation. Obviously if you aren't using a navigation controller, you would need to pass in a different view. 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(经他的许可发布),可在麻省理工学院(MIT)许可下在github上找到: https://github.com/jaredsinclair/JTSImageViewController

Much of this code was adapted from Jared Sinclair's JTSImageViewController (posted with his permission), available under the MIT license on github here: https://github.com/jaredsinclair/JTSImageViewController

这篇关于应该在视图控制器上未调用shouldAutorotate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:32