iOS tableHeaderView使用frame和Masonry对比
tableHeaderView 对大家都不陌生,一直用到。但使用frame设置和Masonry其实是有差别的。先简述下我遇到的问题
自动布局设置tableHeaderView,滚动会有问题,但tableHeaderView内部使用Masonry是可以的
一、懒加载方式创建tableHeaderView(适合比较复杂的布局,或高度动态变化,时有时无,代码封装,可读性比较好)
1、懒加载创建
- (ICKTrendTopMsgView *)topView { if (!_topView) { _topView = [[ICKTrendTopMsgView alloc] init]; WeakObj(self); _topView.btnClick = ^{ Weakself.iCKTiltleModel.msgBottomCount = nil; ICKDymMsgListViewController *vc = [ICKDymMsgListViewController new]; vc.unreadMsgIdSet = Weakself.iCKTiltleModel.unReadMessageIdList; [Weakself pushViewController:vc]; if (Weakself.msgBtnClick) Weakself.msgBtnClick(); }; } return _topView; }
2、设置tableHeaderView
// topview 不随tableview滚动 // [self.view addSubview:self.topView]; // [self.topView mas_remakeConstraints:^(MASConstraintMaker *make) { // make.left.equalTo(self.view.mas_left); // make.top.equalTo(self.view.mas_top); // make.height.mas_equalTo(0); // make.width.mas_equalTo(LL_ScreenWidth); // }]; // // [mainTableView mas_remakeConstraints:^(MASConstraintMaker *make) { // make.left.equalTo(self.view.mas_left); // make.top.equalTo(self.topView.mas_bottom); // make.bottom.equalTo(self.view.mas_bottom); // make.width.mas_equalTo(LL_ScreenWidth); // }]; // tableHeaderView 0.01的时候才有效果 topview 随tableview滚动 // frame self.topView.frame = CGRectMake(0, 0, LL_ScreenWidth, 0.01); mainTableView.tableHeaderView = self.topView; // 自动布局设置tableHeaderView 会有问题 // mainTableView.tableHeaderView = self.topView; // [self.topView mas_remakeConstraints:^(MASConstraintMaker *make) { // make.height.mas_equalTo(0.01); // make.width.mas_equalTo(LL_ScreenWidth); // }]; [mainTableView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.mas_left); make.top.equalTo(self.view.mas_top); make.bottom.equalTo(self.view.mas_bottom); make.width.mas_equalTo(LL_ScreenWidth); }];
3、动态变换tableHeaderView高度
- (void)setICKTiltleModel:(ICKTiltleModel *)iCKTiltleModel { _iCKTiltleModel = iCKTiltleModel; if (iCKTiltleModel.msgBottomCount.integerValue) { NSString *str = [NSString stringWithFormat:@"%@条新消息",ObjErrorCheck(iCKTiltleModel.msgBottomCount)]; [self.topView.btn setTitle:str forState:UIControlStateNormal]; self.topView.btn.hidden = NO; self.topView.height = 54; [self.topView.btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:10]; } else { self.topView.btn.hidden = YES; // topview 不随tableview滚动 // [self.topView mas_updateConstraints:^(MASConstraintMaker *make) { // make.height.mas_equalTo(0); // }]; self.topView.height = 0.01; } // 更新frame之后要重新设置tableHeaderView // frame mainTableView.tableHeaderView = self.topView; }
小记:
1、topview 不随tableview滚动 使用 [self.view addSubview:self.topView];可以正常设置高度,高度变化后直接更新topview高度即可
2、topview 随tableview滚动 使用 mainTableView.tableHeaderView = self.topView; 且用frame方式并且高度要0.01才有效果,高度变化后更新topview高度同时再次设置tableHeaderView
二、直接创建tableHeaderView(适合比较简单的表头,高度固定)
UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0,0,0,135)]; bgView.backgroundColor = [UIColor clearColor]; mainTableView.tableHeaderView = bgView; UIView *contentView = [[UIView alloc]init]; contentView.backgroundColor = XZWL_COLOR_FFFFFF; [bgView addSubview:contentView]; [contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgView).offset(10); make.left.equalTo(bgView).offset(10);; make.right.equalTo(bgView).offset(-10);; make.bottom.equalTo(bgView); }]; [self.view setNeedsLayout]; [self.view layoutIfNeeded]; [contentView setCornerOnTop:8]; UIImageView *thingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"carrer_circle_value_header"]]; thingsImage.contentMode = UIViewContentModeScaleAspectFit; [contentView addSubview:thingsImage]; [thingsImage mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(contentView); make.bottom.equalTo(contentView); }];