NSLayoutConstraint

自己实践出来的,写个总结。

+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullableid)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

此方法各参数:

第一个参数:指定约束的视图view1

第二个参数:指定view1的属性attr1

第三个参数:指定左右两边的视图的关系relation

第四个参数:指定约束右边的视图view2

第五个参数:指定view2的属性attr2

第六个参数:指定一个与view2属性相乘的乘数multiplier

第七个参数:指定一个与view2属性相加的浮点数c

属性说明:

目前为用过的有

    NSLayoutAttributeLeft -->左边约束

    NSLayoutAttributeRight-->右边约束

    NSLayoutAttributeTop-->上边约束

    NSLayoutAttributeBottom-->下边约束

    NSLayoutAttributeLeading-->左边约束

    NSLayoutAttributeTrailing-->又边约束

    NSLayoutAttributeWidth-->宽度约束

    NSLayoutAttributeHeight-->高度约束

    NSLayoutAttributeCenterX-->水平约束

    NSLayoutAttributeCenterY-->垂直约束

relation说明:  

    NSLayoutRelationLessThanOrEqual-->小于等于

    NSLayoutRelationEqual -->等于

    NSLayoutRelationGreaterThanOrEqual -->大于等于


依据的公式是:view1.attr1 = view2.attr2*m +c

以下是自定义几个ScrollView随window的大小变化而变化。

int customColumn = 4;
    int customRow =4;
    NSMutableArray *customviewArray = [[NSMutableArrayalloc] initWithCapacity:0];
    for (int i =0; i< 4; i++) {
        for (int j=0; j < 4; j++) {
            NSScrollView *aview = [NSScrollViewnew];
            [aview setTranslatesAutoresizingMaskIntoConstraints:NO];
            [_myviewaddSubview:aview];
            NSLayoutConstraint *bottom ;
            if (i ==0) {
                bottom = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeBottommultiplier:1.0fconstant:-1.0f];
            }else{
                //                CustomSlotView *bottomview = [customviewArray objectAtIndex:i*customColumn+j-customColumn];
                NSScrollView *bottomview = [customviewArrayobjectAtIndex:i*customColumn+j-customColumn];
                bottom = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:bottomview attribute:NSLayoutAttributeTopmultiplier:1.0fconstant:-10.0f];
                //                [bottomview removeConstraints:[bottomview constraints]];
            }
            NSLayoutConstraint *top ;
            if (i == customRow -1) {
                top = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeTopmultiplier:1.0fconstant:0.0f];
            }
            
            NSLayoutConstraint *left;
            if (j ==0) {
                left = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeLeadingmultiplier:1.0fconstant:1.0f];
            }else{
                //                CustomSlotView *leftview = [customviewArray objectAtIndex:i*customColumn+j-1];
                NSTextView *leftview = [customviewArrayobjectAtIndex:i*customColumn+j-1];
                left = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:leftview attribute:NSLayoutAttributeTrailingmultiplier:1.0fconstant:10.0f];
                //                [leftview removeConstraints:[leftview constraints]];
            }
            NSLayoutConstraint *right;
            if (j == customColumn -1) {
                right = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeTrailingrelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeTrailingmultiplier:1.0fconstant:0.0f];
            }
            NSLayoutConstraint *width;
            NSLayoutConstraint *height;
            if (i*customColumn + j >0) {
                //                CustomSlotView *nextView = [customviewArray objectAtIndex:i*customColumn+j-1];
                NSTextView *nextView = [customviewArrayobjectAtIndex:i*customColumn+j-1];
                width = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nextView attribute:NSLayoutAttributeWidthmultiplier:1.0fconstant:0.0f];
                height = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nextView attribute:NSLayoutAttributeHeightmultiplier:1.0fconstant:0.0f];
                //                [nextView removeConstraints:[nextView constraints]];
            }
            
            bottom.active =YES;
            
            top.active =YES;
            
            left.active =YES;
            right.active =YES;
            
            height.active =YES;
            width.active =YES;
            [customviewArray addObject:aview];
        }
        
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值