Autolayout: change constraint multiplier

Mon, 03. Aug 2015

Categories: en development Tags: Apple Autolayout iOS NSLayoutConstraint Objective C

the NSLayoutConstraint multiplier property is read-only. But if you need to change it, you can replace the constraint with a modified clone like

 1@implementation NSLayoutConstraint(ChangeMultiplier)
 2// visal form center http://stackoverflow.com/a/13148012/349514
 3-(NSLayoutConstraint *)constraintWithMultiplier:(CGFloat)multiplier
 4{
 5  return [NSLayoutConstraint
 6    constraintWithItem:self.firstItem
 7    attribute:self.firstAttribute
 8    relatedBy:self.relation
 9    toItem:self.secondItem
10    attribute:self.secondAttribute
11    multiplier:multiplier
12    constant:self.constant];
13}
14@end

and replace it like

1NSLayoutConstraint *c = [self.constraintToChange constraintWithMultiplier:0.75];
2  [self.view removeConstraint:self.constraintToChange];
3  [self.view addConstraint:self.constraintToChange = c];
4  [self.view layoutIfNeeded];