简体   繁体   English

以编程方式编辑NSLayoutConstraint常量吗?

[英]Edit NSLayoutConstraint Constants programmatically?

I have an NSLayoutConstraint with an IBOutlet connected. 我有一个连接了IBOutletNSLayoutConstraint In my XIB the constraint has a constant value an several others for specific layouts. 在我的XIB ,约束具有恒定值,对于特定布局,约束具有其他几个常数。 For example, Compact Width x Any Height. 例如,“紧凑宽度x任何高度”。

Programmatically I know I can edit the constant like so: 我以编程方式知道我可以像这样编辑常量:

myConstraint.constant = 10;

However, I want to edit the additional constants for the various layouts. 但是,我想编辑各种布局的其他常量。 How can I do this? 我怎样才能做到这一点?

You can use install and uninstall feature. 您可以使用安装和卸载功能。 For example let say we have a width constraint which is install in hAny/wCompact and we create IBOulet for this constraint. 例如,假设我们有一个宽度约束,该约束安装在hAny / wCompact中,并为此约束创建IBOulet。 Then it will have affect on hAny/wCompact. 然后它将对hAny / wCompact产生影响。 Then we can go to other size class say hCompact/wCompact. 然后我们可以转到其他尺寸类,例如hCompact / wCompact。 and Make previous constraint uninstall and create a new width constraint in hComp/wComp. 并卸载先前的约束并在hComp / wComp中创建一个新的宽度约束。 We can create a new IBOutlet for the newly created constraint. 我们可以为新创建的约束创建一个新的IBOutlet。 And it has affect on hComp/wComp. 它对hComp / wComp有影响。

myConstraintInOneSizeClass.constant = 10;
myConstraintInOtherSizeClass.constant = 20;

They are install in their own size classes 它们以自己的尺寸等级安装

Install and uninstall a constraint for different size classes: 安装和卸载不同大小级别的约束:

https://developer.apple.com/library/ios/recipes/xcode_helpIB_adaptive_sizes/chapters/EnableAndDisableConstraints.html https://developer.apple.com/library/ios/recipes/xcode_helpIB_adaptive_sizes/chapters/EnableAndDisableConstraints.html

Hope it can help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM