Set AutoLayout Size Class Programmatically?(以编程方式设置 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 引入了 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
中激活/停用您需要的任何一组约束
- 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 大小类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式设置 AutoLayout 大小类?


基础教程推荐
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01