AutoLayout代码布局


-(void)configueUIWithOrders:(NSMutableArray *)orders{

    

    self.ordersCount = orders.count;

    NSInteger totalPrice =0;

    OrderModel *typeModel = orders[0];

    if ([typeModel.orderTypeisEqualToString:@"wait"]) {

        

        self.payTypeLabel.text =@"待支付";

    }elseif ([typeModel.orderTypeisEqualToString:@"work"]){

        

        self.payTypeLabel.text =@"服务中";

    }else{

        //已完成

        self.payTypeLabel.text =@"已完成";

    }

    

    /************ configue子订单*****************/

    


    for (int i =0; i <self.ordersCount; i++) {

        

        OderSubItem * subOrder = [[[NSBundlemainBundle]loadNibNamed:@"OderSubItem"owner:selfoptions:nil]firstObject];

        OrderModel *subOrderModel = orders[i];


        subOrder.orderName.text = subOrderModel.orderName;

        subOrder.orderDesc.text = subOrderModel.orderDesc;

        //订单总价

        totalPrice += subOrderModel.orderPrice;

        subOrder.tag =1001 + i;

        [self.orderLabelsContentaddSubview:subOrder];

        //不设置为No会把AutoresizingMask自动转化成AutoLayout,必须写在添加到父View之后设置

        [subOrder setTranslatesAutoresizingMaskIntoConstraints:NO];

      }

    

    //设置约束

    [selfsetConstraintWithSubView];


    self.ordersTotalLabel.text = [@(totalPrice)stringValue];

}

- (void)setConstraintWithSubView{

    

    for (int i =0; i <self.ordersCount; i++) {

        //在取父View上的subVIew时要以后加的View.tag的为基准,否则会造成在设置最后一个subView的Constraint的时候发现它的nextView不存在

constraint的设置则要以上一个subView为标准

        UIView * nextOrderView = [self.orderLabelsContentviewWithTag:1001 + i];

        UIView * lastOrderView = [[UIViewalloc]init];

        

        NSLayoutConstraint *subOrderTop = [[NSLayoutConstraintalloc]init];

        

        if (i ==0) {

            

            lastOrderView = self.orderLabelsContent;

            subOrderTop = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:lastOrderViewattribute:NSLayoutAttributeTopmultiplier:1constant:10.0f];

        }else{

            

            lastOrderView = [self.orderLabelsContentviewWithTag:1001 + i -1];

            

            subOrderTop = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:lastOrderViewattribute:NSLayoutAttributeBottommultiplier:1constant:10.0f];


        }


        

         NSLayoutConstraint *subOrderLeft = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:self.orderLabelsContentattribute:NSLayoutAttributeLeadingmultiplier:1constant:0];

        

         NSLayoutConstraint *subOrderRight = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeTrailingrelatedBy:NSLayoutRelationEqualtoItem:self.orderLabelsContentattribute:NSLayoutAttributeTrailingmultiplier:1constant:0];

        

         NSLayoutConstraint *subOrderHeight = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1constant:40];


        [self.orderLabelsContentaddConstraints:@[subOrderLeft,subOrderRight,subOrderTop,subOrderHeight]];

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值