NSLayoutConstraint 使用代码实现约束的添加和删除

来源:互联网 发布:linux怎么装vnc 编辑:程序博客网 时间:2024/06/05 22:37

- (void)addMasonry:(NSView *)view superView:(NSView *)sView padding:(NSEdgeInsets)padding{

    view.translatesAutoresizingMaskIntoConstraints =NO;

    NSLayoutConstraint *top = [NSLayoutConstraintconstraintWithItem:view attribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:sView attribute:NSLayoutAttributeTopmultiplier:1.0constant:padding.top];

    NSLayoutConstraint *bottom = [NSLayoutConstraintconstraintWithItem:view attribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:sView attribute:NSLayoutAttributeBottommultiplier:1.0constant:padding.bottom];

    NSLayoutConstraint *left = [NSLayoutConstraintconstraintWithItem:view attribute:NSLayoutAttributeLeftrelatedBy:NSLayoutRelationEqualtoItem:sView attribute:NSLayoutAttributeLeftmultiplier:1.0constant:padding.left];

    NSLayoutConstraint *right = [NSLayoutConstraintconstraintWithItem:view attribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:sView attribute:NSLayoutAttributeRightmultiplier:1.0constant:padding.right];

    [sView addConstraint:top];

    [sView addConstraint:bottom];

    [sView addConstraint:left];

    [sView addConstraint:right];

}

- (void)removeMasonry:(NSView *)view superView:(NSView *)sView{

    NSArray *arr = [sViewconstraints];

    NSMutableArray *marr = [NSMutableArrayarray];

    for (NSLayoutConstraint  *constranitin arr) {

        if(constranit.firstItem == view){

            [marr addObject:constranit];

        }

    }

    [NSLayoutConstraintdeactivateConstraints:marr];

}

0 0
原创粉丝点击