情况是这样的,需要在scrollveiw上,add一个label,这个label。是根据字符串多少来显示高度的。而scrollview需要更具label来确定contentSize的高度
先上图显示效果
1.初始话scrollview,contentSize高度随便给
- (UIScrollView *)scrollView {
if (!_scrollView ) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, 0);
_scrollView.backgroundColor = COLOR_B8;
}
return _scrollView;
}
# 2.布局之前计算label的高度
CGFloat detailHeight = [self getLabelHeight:self.labDetail];
- (CGFloat)getLabelHeight:(UILabel *)label {
CGRect r = [label.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 40,10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:label.font} context:nil];
return r.size.height;
}
# 3.自动布局,我的这个工程中,前面两个view的高度是写死的。就不说了。主要是第三个view
//label的父视图布局
[self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleView.mas_bottom).offset(10);
make.width.mas_equalTo(_scrollView.frame.size.width - 20);
make.left.equalTo(self.coverView.mas_left);
make.bottom.equalTo(self.scrollView.mas_bottom).offset(-10);
}];
//labe的布局
[self.labDetail mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(labCourseDetail);
make.top.equalTo(labCourseDetail.mas_bottom).offset(10);
make.right.equalTo(self.detailView.mas_right).offset(-10);
make.bottom.equalTo(self.detailView.mas_bottom).offset(-20);
make.height.equalTo(detailHeight);(这里需要bottom和height两个约束)
}];
这样就可以了。scrollview就会根据label的高度变化来显示
联系:QQ925889828