masonry实现根据数组动态添加view,父view高度自适应

KapReplyItemView.h

#import "ViewBase.h"
#import "KapNumberHView.h"
@interface KapReplyItemView : ViewBase
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) LabelBase *titleLabel;
@property (nonatomic, strong) LabelBase *timeLabel;
@property (nonatomic, strong) KapNumberHView *praiseCountView;
@property (nonatomic, strong) LabelBase *contentLabel;
@property (nonatomic, strong) UIView *lineView;
@end

KapReplyItemView.m

#import "KapReplyItemView.h"

@implementation KapReplyItemView
- (void)bankViewInit{
    [self addSubview:self.headerImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.timeLabel];
    [self addSubview:self.praiseCountView];
    [self addSubview:self.contentLabel];
    [self addSubview:self.lineView];
    float leftOffset = PCH_BitMap_BY_SIZE(40);
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        float height = PCH_BitMap_BY_SIZE(70);
        float topOffset = PCH_BitMap_BY_SIZE(40);
        make.top.equalTo(self).offset(topOffset);
        make.left.equalTo(self).offset(leftOffset);
        make.width.height.mas_equalTo(height);
    }];
    [self.praiseCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        float height = PCH_BitMap_BY_SIZE(60);
        float width = PCH_BitMap_BY_SIZE(100);
        make.top.equalTo(self.headerImageView);
        make.right.equalTo(self).offset(-leftOffset);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerImageView);
        make.height.equalTo(self.headerImageView).multipliedBy(0.5);
        make.left.equalTo(self.headerImageView.mas_right).offset(leftOffset);
        make.right.equalTo(self.praiseCountView.mas_left).offset(-leftOffset);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.bottom.equalTo(self.headerImageView);
        make.left.width.equalTo(self.titleLabel);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        float topOffset = PCH_BitMap_BY_SIZE(40);
        float height = PCH_BitMap_BY_SIZE(30);
        make.top.equalTo(self.headerImageView.mas_bottom).offset(topOffset);
        make.left.equalTo(self.headerImageView);
        make.right.equalTo(self.titleLabel);
        make.height.mas_equalTo(height);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        float topOffset = PCH_BitMap_BY_SIZE(20);
        float height = PCH_BitMap_BY_SIZE(1.5);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(topOffset);
        make.left.equalTo(self.headerImageView);
        make.right.equalTo(self);
        make.height.mas_equalTo(height);
    }];
}

PCH_LazyLoadingBlock(UIImageView, headerImageView, {
    headerImageView.image = [UIImage imageNamed:@"custom_icon"];
})

PCH_LazyLoadingBlock(LabelBase, titleLabel, {
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.textColor = PCH_Bank_Black_2a2a2a;
    titleLabel.font = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(30)];
    titleLabel.text = @"nemo";
})

PCH_LazyLoadingBlock(LabelBase, timeLabel, {
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.textColor = PCH_BANK_GRAY_979696;
    timeLabel.font = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(22)];
    timeLabel.text = @"2017.07.22 13:58:06";
})

PCH_LazyLoadingBlock(LabelBase, contentLabel, {
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.textColor = PCH_BANK_GRAY_2f2f2f;
    contentLabel.font = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(24)];
    contentLabel.text = @"评论了你的<去旅行>";
})

PCH_LazyLoadingBlock(KapNumberHView, praiseCountView, {
    praiseCountView.countLabel.font = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(18)];
    praiseCountView.countLabel.textColor = [UIColor whiteColor];
    praiseCountView.countLabel.text = @"16";
    praiseCountView.countLabel.textAlignment = NSTextAlignmentLeft;
})

PCH_LazyLoadingBlock(UIView, lineView, {
    lineView.backgroundColor = PCH_BANK_GRAY_e7e7e7;
})

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

KapReplyView.h

#import "ViewBase.h"
#import "KapReplyItemView.h"

@interface KapReplyView : ViewBase{
    KapReplyItemView *_lastView;
}
// 这个是外部添加的约束的copy
@property (nonatomic,copy) void (^customBlock)(MASConstraintMaker *make);
@property (nonatomic,readonly)KapReplyItemView *lastView;
@property (nonatomic,strong)UIView *lineView;//
@property (nonatomic,strong)NSArray *modelArray;// 回复
@property (nonatomic,strong)NSMutableArray *itemViewsArray;
@end

KapReplyView.m

#import "KapReplyView.h"

@implementation KapReplyView
- (void)bankViewInit{
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        float height = PCH_BitMap_BY_SIZE(2);
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(height);
    }];
}
- (NSArray *)mas_makeConstraints:(void (^)(MASConstraintMaker *))block{
    self.customBlock = block;
    return [super mas_makeConstraints:^(MASConstraintMaker *make) {
        block(make);
        make.bottom.equalTo(self.lineView);
    }];
}
- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    [self cleanSubItemView];
    for (id model in modelArray) {
        KapReplyItemView *view = [self createdItemByModel:model];
        [self addSubview:view];
        [self.itemViewsArray addObject:view];
        float height = PCH_BitMap_BY_SIZE(200);
        if(!_lastView){
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_lastView.mas_bottom);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }
        _lastView = view;
    }
    if (!self.lastView) {
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            self.customBlock(make);
            make.bottom.equalTo(self.lineView);
        }];
        return;
    }
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        self.customBlock(make);
        make.bottom.equalTo(self.lastView);
    }];
}
- (void)cleanSubItemView{
    _lastView = nil;
    for (UIView *view in self.itemViewsArray) {
        [view removeFromSuperview];
    }
    [self.itemViewsArray removeAllObjects];
}
- (KapReplyItemView *)createdItemByModel:(id)model{
    KapReplyItemView *itemView = [[KapReplyItemView alloc] init];
    // 赋值
    return itemView;
}
#pragma mark set 
- (void)setBackgroundColor:(UIColor *)backgroundColor{
    [super setBackgroundColor:backgroundColor];
    self.lineView.backgroundColor = backgroundColor;
}
#pragma mark get
PCH_LazyLoadingBlock(NSMutableArray, itemViewsArray, {})
PCH_LazyLoadingBlock(UIView, lineView, {
})

使用

[self.replyView mas_makeConstraints:^(MASConstraintMaker *make) {
        float topOffset = PCH_BitMap_BY_SIZE(30);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(topOffset);
        make.left.equalTo(self.headerImageView);
        make.right.equalTo(self.titleLabel);
    }];
self.replyView.modelArray = array;

你可能感兴趣的:(masonry实现根据数组动态添加view,父view高度自适应)