【问题标题】:shouldAutorotate not called on view controller视图控制器上未调用 shouldAutorotate
【发布时间】:2015-09-12 05:00:08
【问题描述】:

我有一个由单个视图控制器组成的简单应用程序。我从 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
}

在我的信息列表中,我在 Supported Interface Orientations 下指定了所有方向,并且应用会旋转到 iPad 上的所有方向。

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

示例项目here(需要 Swift 2)


¹非UINavigationController

【问题讨论】:

    标签: ios uiviewcontroller ios9 screen-rotation


    【解决方案1】:

    根据 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

    【讨论】:

    • 谢谢。实际上,我也许可以帮助您进行转角。在-viewWillTransitionToSize:withTransitionCoordinator: 中,调用[UIView setAnimationsEnabled:NO];,然后是[coordinator animateAlongsideTransition:nil completion:...,并在完成时调用[UIView setAnimationsEnabled:YES];
    • 谢谢。我喜欢 Apple 文档的样子 ∞?
    【解决方案2】:

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

    【讨论】:

    • 我尝试了几个小时来让 autoRotate 工作,结果成功了。我欠你一个披萨。
    【解决方案3】:

    我刚刚尝试了这个线程中的解决方案: https://stackoverflow.com/a/32782517/1679627

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

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 2014-11-07
      • 2016-07-05
      • 2011-04-01
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 2018-03-03
      • 2023-04-07
      相关资源
      最近更新 更多