在纵向模式下维护一个视图控制器,在iOS模式下维护其他视图

我有一个iOS应用程序与2视图控制器即FirstViewControllerSecondViewController我的窗口的rootViewController是UINavigationController

FirstViewController只能在纵向模式下工作,而SecondViewController只能在横向模式下工作。

search遍历Stackoverflow我发现,对于iOS6和以上,我必须创build一个类别超过UINavigationController和覆盖-supportedInterfaceOrientations

问题

从FirstViewController开始。 现在我的手机在肖像模式,我推SecondViewController,视图加载在肖像模式。 一旦我将手机旋转到横向,视图将旋转到横向(从这一点起,将不会返回到纵向)。

当我回弹FirstViewController将再次在肖像(不pipe手机的方向)。

我希望SecondViewController不应该以纵向模式显示。 我整天绞尽脑汁……找不到解决办法。

的appdelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController *vc = [[ViewController alloc] init]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc]; [self.navigationController setNavigationBarHidden:YES]; self.window.rootViewController = self.navigationController; [self.window makeKeyAndVisible]; return YES; } 

FirstViewController

 - (IBAction)btnClicked:(id)sender { SecondViewController *vc = [[SecondViewController alloc] init]; [self.navigationController pushViewController:vc animated:NO]; } #pragma mark - Rotation handlers - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } -(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

SecondViewController

 - (IBAction)btnClicked:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - Rotation handlers - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight; } -(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight; } 

UINavigation类别

 @implementation UINavigationController (Rotation) -(BOOL)shouldAutorotate { //return [self.topViewController shouldAutorotate]; return YES; } -(NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.topViewController preferredInterfaceOrientationForPresentation]; } @end 

那有点晚了,但这是我的想法。

虽然有很多解决scheme可用的情况下,当FirstVC是肖像和第二可以是肖像和风景,我找不到任何这个问题的解决scheme(仅第一个肖像和第二个风景)。 这是我做的:

将两个视图控制器embedded到自己的导航控制器中。 创build两个新类,比如说FirstNavController和SecondNavController子类化UINavigationController。 使用这些作为您的导航控制器。 (如果您正在使用StoryBoards,请select导航控制器,转到Identity Inspector并更改“Class”字段)。

现在,在FirstNavController中,添加:

 - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

在SecondNavController中:

 - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } 

您将不得不以模态方式呈现SecondNavController。

你的视图控制器没有什么需要完成的。 确保您在应用程序设置中添加了所有需要的方向。

这种方法唯一的缺点是两个视图不在同一个导航堆栈中,因为第二个是以模态方式显示的,所以你不会看到后退button。 但是你可以自己添加一个取消/解除button,并在SecondVC中调用dismissViewControllerAnimated。

在我以前的应用程序,我已经做到了

首先您需要启用纵向,横向左侧,横向右侧方向才能投影

现在

设置下面的代码到你的FirstViewController

 -(void) viewWillAppear:(BOOL)animated{ [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; } - (BOOL)shouldAutorotate { return NO; } 

设置下面的代码到你的secondViewController

 #define degreesToRadian(x) (M_PI * (x) / 180.0) @implementation UINavigationController (Rotation_IOS6) -(BOOL)shouldAutorotate { return [[self.viewControllers lastObject] shouldAutorotate]; } -(NSUInteger)supportedInterfaceOrientations { return [[self.viewControllers lastObject] supportedInterfaceOrientations]; } @end @interface secondViewController () @end @implementation secondViewController -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); } - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } @end