一: 第1次踩坑:
设置headerview 约束
#import "EditorHeaderView.h"
- (void)bankViewInit{
[self addSubview:self.titleLabel];
[self addSubview:self.contentPromptLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self);
make.left.right.equalTo(self);
make.height.mas_equalTo(140);
}];
[self.contentPromptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.mas_bottom);
make.left.right.equalTo(self);
make.height.mas_equalTo(100);
// 自适应高度
make.bottom.equalTo(self);
}];
// self 自适应高度
CGFloat height = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
将headerview设置为tableview头视图
EditorHeaderView *headerView = [[EditorHeaderView alloc] init];
self.tableView.tableHeaderView = headerView;
一: 第2次踩坑:
代码如下:
/** tableview-HeaderView */
self.positionTableView.tableHeaderView = self.tableHeaderView;
[self.tableHeaderView addSubview:self.netWorthView];
[self.tableHeaderView addSubview:self.calendarView];
[self.tableHeaderView addSubview:self.accountInfoView];
[self.tableHeaderView addSubview:self.positionPromptLable];
[self.netWorthView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.tableHeaderView);
make.left.equalTo(self).offset(leftOffset);
make.right.equalTo(self).offset(-leftOffset);
make.height.mas_equalTo(CF_BitMap_BY_SIZE(340));
}];
[self.calendarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.netWorthView.mas_bottom);
make.left.equalTo(self.positionTableView).offset(leftOffset);
make.right.equalTo(self.positionTableView).offset(-leftOffset);
make.height.mas_equalTo(CF_BitMap_BY_SIZE(5));
}];
[self.accountInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.calendarView.mas_bottom);
make.left.equalTo(self.positionTableView).offset(leftOffset);
make.right.equalTo(self.positionTableView).offset(-leftOffset);
make.height.mas_equalTo(CF_BitMap_BY_SIZE(5));
}];
[self.positionPromptLable mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.accountInfoView.mas_bottom);
make.left.equalTo(self.positionTableView).offset(leftOffset);
make.right.equalTo(self.positionTableView).offset(-leftOffset);
make.height.mas_equalTo(CF_BitMap_BY_SIZE(5));
make.bottom.equalTo(self.tableHeaderView).priorityHigh();
}];
{
/** 高度自适应 */
UIView *headerView = self.tableHeaderView;
CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = headerView.frame;
headerFrame.size.height = height;
headerView.frame = headerFrame;
self.positionTableView.tableHeaderView = headerView;
}
一: 第3次踩坑:
通过网络请求,获取数据动态改变高度
-(void)bankViewInit{
[self addSubview:self.bgView];
[self.bgView addSubview:self.appBoxView];
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self);
make.bottom.equalTo(self.appBoxView.mas_bottom).offset(CF_BG_VIEW_OFFSET);
}];
[self.appBoxView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bgView).offset(CF_BG_VIEW_OFFSET);
make.left.equalTo(self.bgView).offset(CF_BG_VIEW_OFFSET);;
make.right.equalTo(self.bgView).offset(-CF_BG_VIEW_OFFSET);;
}];
// 样式设置
self.bgView.backgroundColor = CF_COLOR_LINE;
}
#pragma mark set
- (void)setApps:(NSArray<NSString *> *)apps{
if(_apps) return;
_apps = apps;
int count = 3;
float size = self.width/(count+1);
// 计算布局高度
int line = (int)apps.count/count;
if (apps.count%count != 0) line += 1;
float height = (size+CF_BG_VIEW_OFFSET) * line;
[self.appBoxView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(height);
}];
// 九宫格布局
for (NSInteger index = 0; index<apps.count; index++) {
NSString *app = apps[index];
HomeTableHeaderItemView *itemView = [[HomeTableHeaderItemView alloc] initWithFrame:CGRectZero];
[itemView setClickBlock:^{
self.ItemClickBlock(app);
}];
[self.appViewArray addObject:itemView];
[self.appBoxView addSubview:itemView];
}
[self.appViewArray mas_distributeSudokuViewsWithFixedItemWidth:size fixedItemHeight:size warpCount:count topSpacing:0 bottomSpacing:0 leadSpacing:0 tailSpacing:0];
// 计算内容高度
CGFloat contentHeight = [self.bgView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect frame = self.frame;
frame.size.height = contentHeight;
self.frame = frame;
}
- (void)setApps:(NSArray<NSString *> *)apps{
_apps = apps;
self.tableView.tableHeaderView = self.headerToolView;
self.headerToolView.apps = apps;
}