即使是风景,iPad模式视图控制器仍然以纵向行事

我在横向模式下呈现ModalViewController有一些困难。 基本上,它显示在适当的界面方向,只有当设备被持有在横向模式,但视图控制器的行为,就像是在肖像模式旋转。

我正在呈现这样的模式

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navController; [self.window makeKeyAndVisible]; LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]]; [self.window.rootViewController presentModalViewController:lvc animated:NO]; return YES; } 

在LoginViewController中

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect(self.view.frame)); return UIInterfaceOrientationIsLandscape(interfaceOrientation); } 

日志结果经过几次旋转:

 2012-03-06 13:51:49.308 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.313 OrientationTest[3132:207] {{0, 20}, {1024, 748}} 2012-03-06 13:51:49.314 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:49.315 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:51.647 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:51.648 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.481 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.482 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.897 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.898 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 

基本上,loginViewController的行为就像在纵向模式下一样。 我在这个视图上有两个文本字段,当我点击其中一个我想要移动视图,所以键盘不显示在文本字段。 为了做到这一点,我不得不修改frame.origin.x而不是y,因为轴是倒置的(视图的行为像肖像),这导致了很多问题。

编辑:

如果我将LoginViewController上的模式表示样式更改为UIModalPresentationPageSheet,它将按预期工作,因此仅全屏模式有问题

第二编辑:

我已经将代码分解为基本的。 我甚至不提出一个模式视图控制器,只是初始化视图控制器作为根视图控制器,仍然发生这种情况。

应用程序委托:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } 

在我的视图控制器中:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect( self.view.frame)); return UIInterfaceOrientationIsLandscape(interfaceOrientation); } 

好的,所以我终于find了一个正确的工作方式。 看来,如果你只是创build一个新的视图控制器,并表示它将始终是倒置的。 我发现的修复是把视图控制器放在导航控制器。

 LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]]; UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:lvc]; nvc.navigationBarHidden = YES; lvc.modalPresentationStyle = UIModalPresentationFullScreen; lvc.parentView = self.navController.view; [self.window.rootViewController presentModalViewController:nvc animated:NO]; 

被x / y倒置的框架是一个“特征”;)你缺less一个convertRect:fromView 。 看看在视图之间转换rects和点 。

在你的情况下,你需要将键盘框的矩形转换为视图控制器的主视图。 没有看到你的键盘框架的东西,我不知道确切的,但它可能是这样的:

 [self.view convertRect:keyboardFrame fromView:keyboardView]; 

改变这一点

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect(self.view.frame)); return UIInterfaceOrientationIsLandscape(interfaceOrientation); } 

进入这个

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect(self.view.frame)); return YES; }