首页 > 代码库 > iOS删除AutoLayout

iOS删除AutoLayout

有时候,我们需要动态改变View中AutoLayout里的某个值,比如移动x值,或者y值,改怎么办呢?

下面封装了比较好的方法来删除:摘自 https://github.com/MakeZL/ZLAutoLayout (封装AutoLayout的框架)技术分享


以下用到 ZLLayoutConstraint 是别名 NSLayoutConstraint

typedefNSLayoutConstraint ZLLayoutConstraint;

---------------

删除所有约束

<span style="font-size:14px;">#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];
        }
    }
}</span>

---------------

删除单个约束

<span style="font-size:14px;">#pragma mark - remove single constraint
- (void)removeAutoLayout:(ZLLayoutConstraint *)constraint{
    for (ZLLayoutConstraint *constraint in self.superview.constraints) {
        if ([constraint isEqual:constraint]) {
            [self.superview removeConstraint:constraint];
        }
    }
}
</span>


---------------

删除多个约束

<span style="font-size:14px;">#pragma mark - remove constraints 
- (void)removeAutoLayoutConstraints:(NSArray *)constraints{
    for (ZLLayoutConstraint *constraint in constraints) {
        for (ZLLayoutConstraint *superViewConstraint in self.superview.constraints) {
            if ([superViewConstraint isEqual:constraint]) {
                [self.superview removeConstraint:constraint];
            }
        }
    }
}</span>



祝大家愉快!



iOS删除AutoLayout