【问题标题】:is there anyway to change easly Interface Orientation?无论如何可以轻松更改界面方向?
【发布时间】:2011-10-28 05:39:54
【问题描述】:

我有 MainWindow.xib 文件,我尝试设计第二个 .xib 文件,即 MainWindowLandscape.xib。我有很多 .xib 文件,我分别为纵向和横向设计它们。

我想在 MainWindow.xib 和 MainWindowLandscape.xib(基于 UITabbarController)中分配它们,我的意思是我将在 MainWindow.xib 中分配纵向视图,在 MainWindowLandscape.xib 中分配横向视图。有可能还是最简单的方法是什么?

所有视图(纵向和横向)彼此执行相同的操作。只有 UI 会发生变化。

非常感谢。

【问题讨论】:

    标签: objective-c ios interface orientation


    【解决方案1】:

    你可以通过覆盖来做到这一点

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    

    在与两个 xib 文件关联的视图控制器中。

    具体来说,如果您想强制方向始终为纵向,请执行以下操作:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation      {
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
           return YES;
        return NO;
    }
    

    当您想强制视图控制器始终以横向显示时:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation      { 
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
           return YES;
        return NO;
    }
    

    执行此操作后,您必须记住,控制器只有在满足条件时才会自动旋转。具体来说,对于标签栏控制器,所有内部控制器必须支持给定的方向(即,它们应该像上面一样实现shouldAutorotateToInterfaceOrientation)。

    【讨论】:

    • 我不确定这是否是您的建议,但文档指出-shouldAutorotateToInterfaceOrientation: 的结果应该动态更改。
    • @jtbandes:在我看来,我的shouldAutorotateToInterfaceOrientation 实现是相当静态的。每个实现都特定于控制器类型,如果不清楚的话......
    • 嗯,好的。我只是误解了“当你想强制横向”。
    • 是的,我已将此块添加到我的项目所有视图中,但它不起作用?有没有你知道的有 UITabbarController 和 UINavigationController 的教程?
    • 不知道任何教程,但看看这个:stackoverflow.com/questions/2868132/… - 如果您需要更多帮助,您可以发布一些代码...
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多