xcode iOS - 以编程方式更改约束关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30381559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - changing constraint relation programmatically
提问by j2emanue
given the following constraint in ios programmatically:
以编程方式在 ios 中给出以下约束:
IBOutlet NSLayoutConstraint *myConstraint;
this constraint is linked in interfacebuilder to the following details:
此约束在 interfacebuilder 中链接到以下详细信息:
How do I change the relation attribute programmatically. I tried to look up for a method called setRelation but I don't see it.
如何以编程方式更改关系属性。我试图查找一个名为 setRelation 的方法,但我没有看到它。
回答by i_am_jorf
According to the documentation, relation
is read-only.
根据文档,relation
是只读的。
What you will need to do, I suspect, is to set
我怀疑你需要做的是设置
self.myConstraint.active = NO;
Then make a new NSLayoutConstraint
programmatically using:
然后NSLayoutConstraint
使用以下方法以编程方式创建一个新的:
+ constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
+ constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
And in the process copying values you want to keep, and replacing the relation.
并在此过程中复制要保留的值并替换关系。
Then add it to the view hierarchywhere appropriate.
然后在适当的地方将其添加到视图层次结构中。
回答by Erhan Demirci
You can do like that :
你可以这样做:
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.yellowView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.redView
attribute:NSLayoutAttributeWidth
multiplier:0.75
constant:0.0]];