How to change the value of 'mutiplier' property of NSLayoutConstarint in iOS8

480 Views Asked by At

I use Adaptive layout features for designing an app. I take a IBOutlet of an "Aspect Ratio" constraint . I want to change the value of this Aspect Ratio Value to the double of current value. The problem is that "constraint" property can be set easily from Code, but "multiplier" property is read only property. For Aspect Ratio Change, "multipier" value change is necessary . How Could I do this?.

@property (retain, nonatomic) IBOutlet NSLayoutConstraint *leftImageWidthAspectRatio;

In code

NSLog(@"cell.leftImageWidthAspectRatio:%@ : %lf  %lf",cell.leftImageWidthAspectRatio, cell.leftImageWidthAspectRatio.constant,cell.leftImageWidthAspectRatio.multiplier);

Results that

 cell.leftImageWidthAspectRatio:<NSLayoutConstraint:0x7c9f2ed0 UIView:0x7c9f2030.width == 2*RIFeedThumbImageView:0x7c9f2c90.width> : 0.000000  2.000000
2

There are 2 best solutions below

0
On BEST ANSWER

You’re right—changing the multiplier on an existing constraint isn’t supported. constant is the exception, not the rule. From the docs:

Unlike the other properties, the constant can be modified after constraint creation. Setting the constant on an existing constraint performs much better than removing the constraint and adding a new one that's exactly like the old except that it has a different constant.

What you need to do is what’s described at the end there: replace the existing constraint with an identical-but-with-a-different-multiplier one. Something like this should work:

NSLayoutConstraint *oldConstraint = cell.leftImageWidthAspectRatio;
CGFloat newMultiplier = 4; // or whatever
NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:oldConstraint.firstItem attribute:oldConstraint.firstAttribute relatedBy:oldConstraint.relation toItem:oldConstraint.secondItem attribute:oldConstraint.secondAttribute multiplier:newMultiplier constant:oldConstraint.constant];
newConstraint.priority = oldConstraint.priority;
[cell removeConstraint:oldConstraint];
[cell addConstraint:newConstraint];

Note that cell may be the wrong view for this—it depends where IB decides to put the original constraint. If that doesn’t work, dig up through the superviews of the constrained views (you can check what constraints they have with the constraints property) until you find where it’s ending up.

0
On

An easy solution of this problem , I find like that

  1. Take an another constraint for Width
  2. Change Priority for Aspect Ratio (less than Width Constraint Priority);
  3. Change the width Constraint Value with the help of Aspect Ratio multipile property

@property (nonatomic,weak) IBOutlet NSLayoutConstraint *leftImagewidthConstaint;

@property (retain, nonatomic) IBOutlet NSLayoutConstraint *leftImageWidthAspectRatio;

In code

cell.leftImageWidthAspectRatio.priority=500;
    cell.leftImagewidthConstaint.constant =cell.leftImagewidthConstaint.constant*cell.leftImageWidthAspectRatio.multiplier;

This works successfully