以编程方式设置 AutoLayout 大小类? [英] Set AutoLayout Size Class Programmatically?

查看:20
本文介绍了以编程方式设置 AutoLayout 大小类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 8 和 Xcode 6 中,我们现在在情节提要中拥有屏幕尺寸网格,让我们可以选择尺寸等级.您可以在其中为不同的屏幕尺寸选择布局格式.

With iOS 8 and Xcode 6, in storyboards we now have the screen size grid letting us select a size class. Where you can select layout formatting for the different screen sizes.

我发现这非常有用,因为它允许我设置基本约束,然后为每个屏幕尺寸设置唯一的约束.

I have found this brilliantly helpful, as it allows me to set the base constraints and then unique ones for each screen size.

我的问题是,您能以编程方式执行此操作吗?我像往常一样创建 NSLayoutConstraint,但我需要能够为不同的屏幕尺寸指定不同的约束.

My question is, can you do this programmatically? I create my NSLayoutConstraint as normal but I need to be able to specify different constraints for different screen sizes.

推荐答案

iOS 8 引入了 active 属性.它允许您激活或停用约束.还有一些方法可以激活/停用多个约束.

iOS 8 introduces the active property on NSLayoutConstraint. It allows you to activate or deactivate a constraint. There are also methods to activate/deactivate multiple constraints.

+ (void)activateConstraints:(NSArray *)constraints
+ (void)deactivateConstraints:(NSArray *)constraints

  • 以编程方式创建约束时,将它们保存在数组中.
  • 为您需要的每个布局创建一个数组.
  • willTransitionToTraitCollection
  • 中激活/停用您需要的任何一组约束

    • Keep your constraints in arrays when creating them programmatically.
    • Create an array for each of the layouts you need.
    • Activate/Deactivate whatever set of constraints you need from within willTransitionToTraitCollection
    • 这篇关于以编程方式设置 AutoLayout 大小类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆