【问题标题】:iPad modal view controller acting in portrait even though it's landscapeiPad 模态视图控制器以纵向动作,即使它是横向的
【发布时间】:2012-03-06 11:59:48
【问题描述】:

我在横向模式下呈现 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;
}

在登录视图控制器中

- (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);
}

【问题讨论】:

    标签: iphone objective-c ios ipad rotation


    【解决方案1】:

    好的,所以我终于找到了一种让它正常工作的方法。似乎如果您只是创建一个新的视图控制器并显示它总是会被反转。我发现的解决方法是将视图控制器放在导航控制器中。

    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];
    

    【讨论】:

      【解决方案2】:

      x/y 反转的帧是一个“功能”;)您缺少convertRect:fromView。在视图之间查看converting rects and points

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

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

      【讨论】:

      • 对不起,我可能没听懂你在说什么。我正在使用标准的 iOS 键盘。框架一直是倒置的,所以这是我的问题。
      【解决方案3】:

      改一下

          - (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;
      }    
      

      【讨论】:

      • 它的行为相同。另外我希望它被锁定在横向,所以即使它有效,这也不是我的问题的真正解决方案
      • 您是否将所有视图控制器中的值更改为 YES?
      • 是的,它仍然具有相同的行为。但即使它有效也没有意义,因为该应用程序应该只在横向运行
      • 尝试改成返回 interfaceOrientation = UIInterfaceOrientationLandscapeRight|UIInterfaceOrientationLandscapeLeft;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      相关资源
      最近更新 更多