【问题标题】:portrait and landscape orientation without using auto layout constraints纵向和横向方向,不使用自动布局约束
【发布时间】:2012-10-09 05:37:34
【问题描述】:

我的 iphone 应用程序支持纵向和横向。为了在新的 iphone 5 中做到这一点,iOS 6 引入了一个称为自动布局约束的新属性。但是我需要同时支持我的应用程序 iOS 5 和 iOS 6。所以我不能使用这个约束。有没有办法在不使用自动布局的情况下做到纵向和横向?

【问题讨论】:

    标签: iphone orientation ios6


    【解决方案1】:

    是的,你可以很容易地做到这一点。

    适用于 iOS6

    UIViewControllers,其中只需要PORTRAIT模式,写这些函数

    - (BOOL)shouldAutorotate
    {
         return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
         return (UIInterfaceOrientationMaskPortrait);
    }
    

    对于也需要 LANDSCAPE 的 UIViewController,将遮罩更改为 All。

    - (NSUInteger)supportedInterfaceOrientations
    {
        return (UIInterfaceOrientationMaskAllButUpsideDown);
        //OR return (UIInterfaceOrientationMaskAll);
    }
    

    现在,如果你想在方向改变时做一些改变,那就使用这个功能。

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    
    }
    

    适用于 iOS5

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
        if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            return YES;
        }
    
        return NO;
    }
    

    【讨论】:

    • 谢谢mayuur。但是我如何设置页面元素的框架?在xib中可以吗?
    • 对于不同的方向,您仍然可以从 Xib 设置自动调整大小的元素。对于手动更改,您必须在 iOS5 的 shouldAutorotateToInterfaceOrientation 和 iOS6 的 willRotateToInterfaceOrientation 中写入
    • 哦.. 好的.. 在这种情况下,如果 iOS 5,调用 shouldAutorotateToInterfaceOrientation,如果 iOS 6,调用 willRotateToInterfaceOrientation,我该怎么做?
    • 那件事,iOS 会自己处理!虽然,如果您需要检查,您可以通过 [[UIDevice currentDevice] systemVersion] 获取当前版本
    • 还有一个新方法叫做viewDidLayoutSubviews
    猜你喜欢
    • 2015-03-22
    • 2023-04-05
    • 2013-07-20
    • 1970-01-01
    • 2013-08-15
    • 2017-11-28
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    相关资源
    最近更新 更多