【问题标题】:View Controller: Ladnscape to Portrait视图控制器:横向到纵向
【发布时间】:2012-12-21 01:29:39
【问题描述】:

我的应用中有一个视图控制器。让它的第一个视图控制器,我的第一个视图控制器在手机中以纵向模式出现,当用户以横向模式旋转手机时,第一个视图控制器也以横向模式旋转。

它工作正常,现在我在第一个视图控制器上有一个按钮,当我触摸按钮时第二个视图控制器出现。我只想做的是,即使第一个视图控制器处于横向模式,第二个视图控制器也应该始终以纵向模式出现。 是否有任何方法我必须重写才能获得此功能?

【问题讨论】:

  • 两个视图控制器是如何连接的(例如,导航控制器,...)?
  • 基本上我使用了导航控制器。
  • [self.navigationController pushViewController:secondViewController animated:YES];我就是用这个方法连接的。

标签: iphone ios ipad


【解决方案1】:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

在第二个视图控制器中保留这个。

【讨论】:

    【解决方案2】:

    在导航控制器中,控制器的方向取决于导航控制器的根控制器的方向。

    你有两种可能:

    1. 根据实际显示的控制器,使根控制器的 shouldAutorotateToInterfaceOrientation: 返回不同的值;

    2. 对您的视图控制器的视图使用变换,以便旋转。

    我会尝试第一种方法,开始。查看this post 了解如何操作(只需忽略UITabBarController 的内容),或试试这个(它只是将消息中继到导航层次结构中的顶部控制器):

     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {   
        return [self.navigationController.topController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
     }
    

    为了在iOS6上达到同样的效果,尝试定义以下方法:

    -(NSUInteger)supportedInterfaceOrientations {
      return [self.navigationController.topController supportedInterfaceOrientations];
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
      return [self.navigationController.topController  preferredInterfaceOrientationForPresentation];
    }
    

    【讨论】:

    • 先生,我正在使用 ios6。我创建了 UINavigationController 的控制器子类。以上方法在 ios6 中已弃用。所以我使用了 shouldAutorotate 方法。然后我使用我的第一个视图控制器作为导航控制器的根视图控制器。但它不起作用,先生。
    • 虽然已弃用,但如果您希望您的应用程序在 iOS5 上运行,您仍然需要 shouldAutorotateToInterfaceOrientation - 我只是认为您会将相同的推理应用于 iOS6 方法(我没有在这种情况下使用然而)。无论如何,请检查我对解决方案的 iOS6 版本的编辑。我希望它有效,但正如我所说,我还没有尝试过。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2010-12-26
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多