IOS Masonry自动布局

时间:2022-06-02 00:18:57

之前项目用Frame布局,这个项目登录用了VFL,后来觉得用Masonry,前天布局TableViewCell时用了下 ,觉得还不错。

#import "Masonry.h"
#import "MASViewAttribute.h"

先看效果图:

IOS Masonry自动布局IOS Masonry自动布局

#import "ReportsCell.h"
//#import "Masonry.h"
#import "YZPUIFormatMacros.h"
#import "Global.h"

static const CGFloat kIconWidth                 = 55.0f;
static const CGFloat kIconHeight                = 55.0f;

@implementation ReportsCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
         self.backgroundColor = [UIColor whiteColor];

        UIView *headerView = [[UIView alloc]init];
        headerView.backgroundColor = YZPColorDividingLineOutdide;
        [self addSubview:headerView];
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@);
            make.top.equalTo(@);
            make.width.equalTo(@(MainWidth));
            make.height.equalTo(@(YZPSpaceHSmall));
        }];
        UIView *topLine=[self lineView];
        [headerView addSubview:topLine];
        [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView.top);
            make.left.equalTo(headerView.left);
            make.width.equalTo(headerView.width);
            make.height.equalTo(@0.5);

        }];

        UIView *bottomLine=[self lineView];
        [headerView addSubview:bottomLine];
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView.bottom).with.offset(-0.5);
            make.left.equalTo(headerView.left);
            make.width.equalTo(headerView.width);
            make.height.equalTo(@0.5);

        }];

        _orderIdLabel=[[UILabel alloc]init];
        _orderIdLabel.font = YZPFontSubhead;
        _orderIdLabel.textColor = YZPColorTextSubhead;
        [self addSubview:_orderIdLabel];
        [_orderIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView.bottom).with.offset();
            make.left.equalTo(@);
            make.width.equalTo(headerView.width);
            make.height.equalTo(@);
        }];

        _orderIconView=[[UIImageView alloc]init];
         [self addSubview:_orderIconView];
        [_orderIconView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@);
            make.top.equalTo(_orderIdLabel.bottom).with.offset();
            make.width.equalTo(@(kIconWidth));
            make.height.equalTo(@(kIconHeight));
        }];

        _orderTitleLabel=[[UILabel alloc]init];
        _orderTitleLabel.font=YZPFontSubhead;
        _orderTitleLabel.textColor=[UIColor colorWithRed:0.141f green:0.141f blue:0.141f alpha:1.00f];
        _orderTitleLabel.numberOfLines=;
        [self addSubview:_orderTitleLabel];
        [_orderTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_orderIconView.right).with.offset();
            make.top.equalTo(_orderIconView.top).with.offset();
            make.width.equalTo(@(MainWidth-));
            make.height.lessThanOrEqualTo(@());
        }];

        _orderSpecLabel=[[UILabel alloc]init];
        _orderSpecLabel.font=YZPFontExplain;
        _orderSpecLabel.textColor=[UIColor colorWithRed:0.400f green:0.400f blue:0.400f alpha:1.00f];
        [self addSubview:_orderSpecLabel];
        [_orderSpecLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_orderTitleLabel.left);
            make.top.equalTo(_orderTitleLabel.bottom).offset();
            make.width.equalTo(@(MainWidth-));
            make.height.lessThanOrEqualTo(@());
        }];

        _productsCountLabel=[[UILabel alloc]init];
        _productsCountLabel.font=YZPFontContent;
        _productsCountLabel.textAlignment=NSTextAlignmentRight;
        _productsCountLabel.textColor=[UIColor colorWithRed:0.400f green:0.400f blue:0.400f alpha:1.00f];
        [self addSubview:_productsCountLabel];
        [_productsCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_orderTitleLabel.top);
            make.left.equalTo(_orderTitleLabel.right).offset();
            make.right.equalTo(self.right).with.offset(-);
            make.height.equalTo(_orderTitleLabel.height);
        }];

        UIView *centerLine=[self lineView];
        [self addSubview:centerLine];
        [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_orderIconView.bottom).offset();
            make.left.equalTo(self);
            make.width.equalTo(self);
            make.height.equalTo(@0.5);
        }];

        _userNameLabel=[[UILabel alloc]init];
        _userNameLabel.font=YZPFontExplain;
        _userNameLabel.textColor=YZPColorTextExplain;
//        _userNameLabel.numberOfLines=2;
        _userNameLabel.textAlignment=NSTextAlignmentLeft;
        [self addSubview:_userNameLabel];
        [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).with.offset();
            make.top.equalTo(centerLine.bottom).offset();
            make.width.lessThanOrEqualTo(@);
            make.height.lessThanOrEqualTo(@);
        }];

        _userPhoneLabel=[[UILabel alloc]init];
        _userPhoneLabel.font=YZPFontExplain;
        _userPhoneLabel.textColor=YZPColorTextExplain;
        _userPhoneLabel.textAlignment=NSTextAlignmentRight;
        [self addSubview:_userPhoneLabel];
        [_userPhoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_userNameLabel.top);
            make.right.equalTo(self.right).with.offset(-);
            make.height.lessThanOrEqualTo(@);
            make.width.lessThanOrEqualTo(@);

        }];

        _userAddressLabel=[[UILabel alloc]init];
        _userAddressLabel.font=YZPFontExplain;
        _userAddressLabel.textColor=YZPColorTextExplain;
        [self addSubview:_userAddressLabel];
        [_userAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_userNameLabel.bottom).with.offset();
            make.left.equalTo(self.left).with.offset();
//            make.bottom.equalTo(self.bottom).with.offset(-10);
            make.width.equalTo(self.width);
            make.height.lessThanOrEqualTo(@);
        }];

    }
    return self;
}
-(UIView *)lineView
{
    UIView *line=[[UIView alloc]init];
//    line.backgroundColor=[UIColor redColor];
    line.backgroundColor=[UIColor colorWithRed:0.800f green:0.800f blue:0.800f alpha:1.00f];
    return line;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end