multiplier
屬性是只讀的,不可以進(jìn)行直接修改。stackoverflow
給出答案,特意分享出來锦积,給有需要的人。原網(wǎng)站在這里
OC寫法
- (void)p_changeMultiplierOfConstraint:(NSLayoutConstraint *)constraint multiplier:(CGFloat)multiplier {
[NSLayoutConstraint deactivateConstraints:@[constraint]];
NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:constraint.firstItem attribute:constraint.firstAttribute relatedBy:constraint.relation toItem:constraint.secondItem attribute:constraint.secondAttribute multiplier:multiplier constant:constraint.constant];
newConstraint.priority = constraint.priority;
newConstraint.shouldBeArchived = constraint.shouldBeArchived;
newConstraint.identifier = constraint.identifier;
[NSLayoutConstraint activateConstraints:@[newConstraint]];
}
調(diào)用
[self p_changeMultiplierOfConstraint:_proImgWidthCtt multiplier:0.5];
Swift寫法
import Foundation
import UIKit
extension NSLayoutConstraint {
/**
Change multiplier constraint
- parameter multiplier: CGFloat
- returns: NSLayoutConstraint
*/
func setMultiplier(multiplier:CGFloat) -> NSLayoutConstraint {
NSLayoutConstraint.deactivate([self])
let newConstraint = NSLayoutConstraint(
item: firstItem,
attribute: firstAttribute,
relatedBy: relation,
toItem: secondItem,
attribute: secondAttribute,
multiplier: multiplier,
constant: constant)
newConstraint.priority = priority
newConstraint.shouldBeArchived = self.shouldBeArchived
newConstraint.identifier = self.identifier
NSLayoutConstraint.activate([newConstraint])
return newConstraint
}
}