supportInterfaceOrientations更改时如何通知系统?
我的根视图控制器的supportedInterfaceOrientations
的实现几乎总是返回UIInterfaceOrientationMaskAll
,但是有一个边界情况返回UIInterfaceOrientationMaskLandscape
。
这是工作,如果用户旋转设备。 但是,如果设备处于纵向模式,则不会调用supportedInterfaceOrientations
方法,除非用户手动旋转设备。
我怎样才能以编程方式告诉系统这个方法的返回值已经改变了?
根据文档,似乎我应该能够调用[UIViewController attemptRotationToDeviceOrientation]
但是这并没有任何效果( supportedInterfaceOrientations
永远不会被调用,屏幕不旋转)。
我发现其他人已经发布了各种解决方法来尝试解决这个问题,但是他们都没有在我的testing中工作。 我怀疑他们可能在iOS 5.0中工作,但不是在iOS 6.0中工作。
我在根视图控制器的shouldAutorotate
方法中返回YES
。
首先,如果你想在横向模式下呈现你的UIViewController的话,这可能是有用的。
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight; }
此外,很多依赖于你的UIViewControllerembedded到哪个控制器。
例如,如果它在UINavigationController里面,那么你可能需要inheritance这个UINavigationController来覆盖像这样的方向方法。
子类UINavigationController(层次结构的顶级视图控制器将控制方向)需要设置为self.window.rootViewController。
- (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } - (NSUInteger)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; }
从iOS 6开始,UINavigationController不会要求UIVIewControllers支持定位。 因此,我们需要inheritance它。
注意 :
每当Push操作完成时, shouldAutorotate
和supportedInterfaceOrientations
方法总是被调用UINavigationController。
从苹果的UIViewController类引用引用:
注意 :在启动时,应用程序应始终以纵向方式设置其界面。 应用程序:didFinishLaunchingWithOptions:方法返回后,应用程序使用上述视图控制器旋转机制在显示窗口之前将视图旋转到适当的方向。
如果界面以纵向开始,即使用户使用侧面的设备打开应用程序,自动旋转也应该能够处理调整。
更新:我发现这个post,应该帮助轮转后发射。 显然,iOS 6会查看导航控制器来确定支持的设备方向。
如何强制iOS 6中的UIViewController纵向
你需要手动旋转它。 你会想要在视图控制器的viewWillAppear:
方法中调用以下逻辑:
UIDeviceOrientation curDevOrientation = [[UIDevice currentDevice] orientation]; if (![self supportsOrientation:curDevOrientation]) { // We're going to rotate 90 degrees clockwise. First figure out what that // means to the status bar. UIInterfaceOrientation newStatusBarOrientation; switch (curDevOrientation) { case UIDeviceOrientationPortrait: newStatusBarOrientation = UIInterfaceOrientationLandscapeRight; break; case UIDeviceOrientationPortraitUpsideDown: newStatusBarOrientation = UIInterfaceOrientationLandscapeLeft; break; } [[UIApplication sharedApplication] setStatusBarOrientation:newStatusBarOrientation animated:NO]; // Now rotate the view 90 degrees clockwise. CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 90.0 / 180.0); self.view.transform = transform; }
这应该旋转特定的视图控制器的视图,只要它出现。