强制纵向模式

好吧,因为没有人回答我之前的问题,我开始相信可能没有简单的方法可以做到这一点。 但我很乐观。 这是我的问题:

在我的应用程序中,我使用常规UIButton从ViewControllerOne切换到ViewControllerTwo。 ViewControllerOne始终处于横向模式。 ViewControllerTwo应该始终处于纵向模式。 但是,当我在我的景观ViewControllerOne中按下按钮时,ViewControllerTwo也处于横向模式,尽管我希望它切换到纵向模式,而不管按下按钮时用户如何旋转设备。

我将以下代码添加到AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { NSUInteger orientations = UIInterfaceOrientationMaskAll; if (self.window.rootViewController) { UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject]; orientations = [presented supportedInterfaceOrientations]; } return orientations; } 

我把它添加到我的ViewController中,它应该只是在纵向模式下:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } -(void)viewWillAppear:(BOOL)animated { UIApplication* application = [UIApplication sharedApplication]; application.statusBarOrientation = UIInterfaceOrientationPortrait; } 

有没有办法告诉viewController进入纵向模式,即使前一个视图是风景? 也许我可以创建一个自定义segue,当按下按钮时会强制视图处于纵向模式? 这里最好/官方解决方案是什么?

根据我对这个问题的理解,我假设您希望第二个视图控制器是纵向的,第一个视图控制器是横向的。

对于第二个viewcontroller,添加此方法:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

对于第一个视图控制器:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscape); } 

检查问题如何处理iOS 6中的不同方向。请参阅那里的答案,了解您需要的项目示例。

基本上,您需要在viewcontroller中嵌入自定义导航控制器(您要旋转的控制器)。 在此自定义导航控制器中添加以下方法(它用于横向,但您可以将其替换为纵向)

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

并添加到应旋转的视图控制器:

 - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; } 

确保在项目中启用纵向,横向右和横向左方向。 然后,如果要阻止特定视图的某些方向:

 – application:supportedInterfaceOrientationsForWindow: