如何以编程方式修改约束乘数?我设置了以下内容:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0]];
我需要将其修改为:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
您将无法更改乘数,因为它是只读的。您必须存储对约束的引用。然后,当您想要更改乘数时,您可以删除约束并添加一个新约束。
您可以在视图控制器实现中包含这样的方法:
- (void)changeMultiplier:(NSLayoutConstraint **)constraint to:(CGFloat)newMultiplier{
NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:(*constraint).firstItem attribute:(*constraint).firstAttribute relatedBy:(*constraint).relation toItem:(*constraint).secondItem attribute:(*constraint).secondAttribute multiplier:newMultiplier constant:(*constraint).constant];
[self.view removeConstraint:(*constraint)];
[self.view addConstraint:newConstraint];
*constraint = newConstraint;
}
相关问题:
我可以更改 NSLayoutConstraint 的 multiplier 属性吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)