有時候,我們需要動態改變View中AutoLayout裡的某個值,比如移動x值,或者y值,改怎麼辦呢?
以下用到 ZLLayoutConstraint 是別名 NSLayoutConstraint
typedef NSLayoutConstraint ZLLayoutConstraint;
---------------
刪除所有約束
#pragma mark - remove view to superView autoLayout
- (void)removeAllAutoLayout{
[self removeConstraints:self.constraints];
for (ZLLayoutConstraint *constraint in self.superview.constraints) {
if ([constraint.firstItem isEqual:self]) {
[self.superview removeConstraint:constraint];
}
}
}
刪除單個約束
#pragma mark - remove single constraint
- (void)removeAutoLayout:(ZLLayoutConstraint *)constraint{
for (ZLLayoutConstraint *constraint in self.superview.constraints) {
if ([constraint isEqual:constraint]) {
[self.superview removeConstraint:constraint];
}
}
}
---------------
刪除多個約束
#pragma mark - remove constraints www.2cto.com
- (void)removeAutoLayoutConstraints:(NSArray *)constraints{
for (ZLLayoutConstraint *constraint in constraints) {
for (ZLLayoutConstraint *superViewConstraint in self.superview.constraints) {
if ([superViewConstraint isEqual:constraint]) {
[self.superview removeConstraint:constraint];
}
}
}
}