shouldAutorotate没有调用视图控制器

我有一个简单的应用程序组成的单个视图控制器。 我开始使用Xcode 7 GM Single View Application模板,但是删除了主要的故事板,并设置了我的视图控制器,如下所示:

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 shouldAutorotate()supportedInterfaceOrientations()方法从不会被调用。 这是一个问题,因为我正在尝试启用和禁用自动旋转的UI控件。 有什么可以阻止这些方法被称为?

这里的示例项目(需要Swift 2)


UINavigationController

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

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

你可以添加一个反转,但不是很好,至less据我所知。 我得到了它的工作,但是当你旋转时无法禁用angular的animation,所以你看起来像在旋转,但是内容不旋转。

这是我用来对抗旋转的代码。 请注意,我必须隐藏状态栏,否则它会旋转,我不知道如何反击。

另外请注意,在self.navigationController.view.superview.superview上做自动旋转可能不是最好的方法,并可能在未来的某个时候打破。 有可能有一个更好的方法来获得正确的视图用于对抗旋转。 显然,如果您不使用导航控制器,则需要传入不同的视图。 因人而异。

 - (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; } 

这些代码中的大部分都是从Jare​​d Sinclair的JTSImageViewController(经他的许可发布)改编而来,可以在github的MIT许可证下find: https : //github.com/jaredsinclair/JTSImageViewController

我只是尝试在这个线程的解决scheme: https : //stackoverflow.com/a/32782517/1679627

请注意,info.plist中有两个支持iPhone的方向和iPad的方向。

要select退出幻灯片和拆分视图,请将UIRequiresFullScreen键添加到Xcode项目的Info.plist文件中,并应用布尔值YES。