UINavigationController强制旋转

我的应用程序主要是肖像,但有一个视图需要横向。

我的意见是包含在一个UINavigationController,这(显然)是这个问题的原因。

所有UIViewControllers除了一个有这样的:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

需要横向的UIViewController有这样的:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 

现在,当用户到达横向UIViewController时会发生什么情况,它是以纵向显示的。 用户可以旋转他们的手机,并根据需要显示在横向(locking到横向)。 用户然后继续前进到一个肖像UIViewController,同样的情况发生:它开始在横向,然后他们旋转他们的手机,并再次成为肖像(locking肖像)。

看起来方向locking是允许的UIViewControllers之间,但自动旋转/编程方式改变方向以某种方式被阻止。

如何强制手机更新到正确的方向?

有一个临时的解决scheme:我可以检测设备的方向,并显示一条消息,要求他们旋转设备,如果不正确,但这不是最佳的。

我有一个我的应用程序相同的要求!

幸运的是我find了解决scheme!

为了保持主视图控制器的景观,不pipe从哪个方向popup/推动,我做了以下事情:(在viewWillAppear 🙂

 //set statusbar to the desired rotation position [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO]; //present/dismiss viewcontroller in order to activate rotating. UIViewController *mVC = [[[UIViewController alloc] init] autorelease]; [self presentModalViewController:mVC animated:NO]; [self dismissModalViewControllerAnimated:NO]; 

在sdk 3.2.5 ios 5.0.1上进行了PST

PS在iOS 8之前的答案导致一些屏幕闪烁,也是不稳定的(在某些情况下,它不再适用于我)。所以,为了我的需要,我将代码更改为:(ARC)

 //set statusbar to the desired rotation position [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO]; [self.navigationController presentViewController:[UIViewController new] animated:NO completion:^{ dispatch_after(0, dispatch_get_main_queue(), ^{ [self.navigationController dismissViewControllerAnimated:NO completion:nil]; }); }]; //I added this code in viewDidDissapear on viewController class, which will be popped back. 

希望它会帮助!

这可能有帮助。 在适当情况下,您可以在出现时调用以下方法。 例如在-viewWillAppear:animated

attemptRotationToDeviceOrientation尝试将所有窗口旋转到设备的方向。

 + (void)attemptRotationToDeviceOrientation 

讨论

某些视图控制器可能希望使用特定于应用程序的条件来确定其实现shouldAutorotateToInterfaceOrientation:方法的返回值。 如果你的视图控制器这样做,当这些条件改变时,你的应用程序应该调用这个类的方法。 系统立即尝试旋转到新的方向。 只要每个相关视图控制器在其实现的shouldAutorotateToInterfaceOrientation:方法中返回YES,就会发生旋转。

可用性

在iOS 5.0及更高版本中可用。 http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html

用这个,

 [[UIDevice currentDevice]performSelector:@selector(setOrientation:) withObject:(__bridge id)((void *)UIInterfaceOrientationLandscapeRight)];