如何使应用程序完全正确工作在iOS 6自动旋转?

在iOS6中,不推荐使用shouldAutorotateToInterfaceOrientation。 我试图使用supportedInterfaceOrientationsshouldAutorotate使应用程序正常自动旋转,但失败。

这个ViewController我不想旋转,但它不工作。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(BOOL)shouldAutorotate { return NO; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

有任何想法吗? 提前感谢您的帮助!

弄清楚了。

1)子类UINavigationController (层次结构的顶级视图控制器将控制方向。)将它设置为self.window.rootViewController。

 - (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } - (NSUInteger)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; } 

2)如果你不想视图控制器旋转

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(BOOL)shouldAutorotate { return NO; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

3)如果你想它能够旋转

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; } -(BOOL)shouldAutorotate { return YES; } 

顺便说一句,根据您的需要,另一个相关的方法:

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; } 

如果使用Tab Bar Controller而不是导航控制器作为根控制器,则需要类似于UITabBarController的子类。

而且语法也会不同。 我用下面的成功。 然后,我使用上面的例子,在我想覆盖的视图控制器上取得成功。 在我的情况下,我想主屏幕不旋转,但我有一个电影的常见问题屏幕,我自然希望启用横向视图。 完美的工作! 只要注意语法更改为self.modalViewController(如果您尝试使用导航控制器的语法,您将得到一个编译器警告。)希望这有助于!

 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BOOL)shouldAutorotate { return self.modalViewController.shouldAutorotate; } - (NSUInteger)supportedInterfaceOrientations { return self.modalViewController.supportedInterfaceOrientations; }