【问题标题】:Xcode Autolayout - View rotated when uncheckedXcode Autolayout - 未选中时视图旋转
【发布时间】:2013-02-25 04:32:09
【问题描述】:

我在 Xcode 4.5 中使用启用自动布局创建了一个应用程序。由于它与 iOS 5 不兼容(在测试之前不知道),我从情节提要中取消选中自动布局。现在在模拟器中测试时视图的内容都是顺时针旋转的。

该应用程序最初是横向的(在 Xcode 中看起来不错)。模拟器将在横向启动,但视图内的所有内容在取消选中自动布局后看起来都已旋转。

示例:

我尝试了一个新的视图控制器,但它仍然像上面那样显示。初始方向已设置为横向。有什么解决办法吗?

【问题讨论】:

    标签: ios objective-c xcode autolayout


    【解决方案1】:

    要解决方向问题,请执行此操作。

    在 .pch 文件中定义这些宏

    #define IOS_OLDER_THAN_6        ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] <  6.0 )
    #define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )
    

    并在 viewContrller.m 中编写此方法

    #ifdef IOS_OLDER_THAN_6
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
        (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    #endif
    
    #ifdef IOS_NEWER_OR_EQUAL_TO_6
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    - (NSUInteger)supportedInterfaceOrientations
    {
        return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
    }
    #endif
    

    【讨论】:

    • 请注意,预处理器指令(#define、#ifdef)在编译时而不是在运行时扩展。这两个宏都是在编译源代码时定义的,因此所有三个方法都将编译到可执行文件中。
    【解决方案2】:

    当涉及到UINavigationController 时,还有一件事,将UINavigationControlleroverriding supportedInterfaceOrientations 子类化。

     #import "UINavigationController+Orientation.h"
    
     @implementation UINavigationController (Orientation)
    
    -(NSUInteger)supportedInterfaceOrientations
    {
       return [self.topViewController supportedInterfaceOrientations];
    }
    
    -(BOOL)shouldAutorotate
     {
        return YES;
     }
    
    @end  
    

    现在,iOS 容器(例如 UINavigationController)不会咨询它们的子容器来确定它们是否应该自动旋转。
    如何子类
    1.添加一个新文件(cocoa touch下Objective c-category)
    2.Category:方向Category On:UINavigationController
    3.将以上代码添加到UINavigationController+Orientation.m

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2016-02-06
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多