iOS删除AutoLayout

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

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


以下用到 ZLLayoutConstraint 是别名 NSLayoutConstraint

typedef NSLayoutConstraint 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>



祝大家愉快!



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。