iOS 6:将视图控制器推入导航控制器堆栈时如何强制更改方向

我觉得这个问题现在应该已经有一百万次了,但是我还是找不到答案。

这是我的层次结构:UINavigationController – > UIViewController 1 – >(push) – > UIViewController 2

UINavigationController:支持所有可能的方向UIViewController 1:仅支持纵向UIViewController 2:仅支持横向

我怎样才能lockingUIViewController 1到肖像只有同时lockingUIViewController 2到景观只? 这甚至有可能吗? 到目前为止,我所看到的是UIViewController 2总是采用UIViewController 1的方向。

请注意,这仅适用于iOS 6。

谢谢!

我也发现同样的问题。我发现应该Autorotate函数不会每次调用,所以我改变方向编程

首先导入这个

#import <objc/message.h> 

然后

 -(void)viewDidAppear:(BOOL)animated { if(UIDeviceOrientationIsPortrait(self.interfaceOrientation)){ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft ); } } } 

希望这对你有所帮助。

添加新的Objective-C类(UINavigationController的子类)并将以下代码添加到.m文件

 -(NSUInteger)supportedInterfaceOrientations { NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController supportedInterfaceOrientations]); return [self.topViewController supportedInterfaceOrientations]; } -(BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // You do not need this method if you are not supporting earlier iOS Versions return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; } 

添加新类之后,转到您的ViewController类并进行以下更改

 - (BOOL)shouldAutorotate // iOS 6 autorotation fix { return YES; } - (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix { return UIInterfaceOrientationMaskAll; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix { return UIInterfaceOrientationPortrait; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } 

在这里输入图像说明

在shouldAutorotate中,shouldAutorotateToInterfaceOrientation:如果您希望ViewController支持多方向,则返回YES;否则返回NO,同样在houldAutorotateToInterfaceOrientation:方法中传递您想要的特定ViewController的Orintation,对所有视图控制器重复相同的操作。

这样做的理由:

1:尽pipe您可以将任何viewController的preferredInterfaceOrientationForPresentation更改为特定的方向,但由于您使用的是UINavigationController,因此还需要覆盖UINavigationController的supportedInterfaceOrientations

2:为了覆盖UINavigationController的supportedInterfaceOrientations,我们已经创build了UINavigationController子类,并修改了与UINavigation Orientation相关的方法。

希望它会帮助你!

使应用程序仅支持纵向模式,并将以下行添加到UIViewController 2的initWithNibName

 self.view.transform = CGAffineTransformMakeRotation(M_PI/2);