在 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 引入了 NSLayoutConstraint 上的 html#//apple_ref/doc/uid/TP40010628-CH1-DontLinkElementID_6" rel="noreferrer">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
willTransitionToTraitCollection
这篇关于以编程方式设置 AutoLayout 大小类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!