使用自适应行高,处理UISCrollView滚动问题
//
// YFTestCell.m
// EMCustomKeyBoardDemo
//
// Created by fank on 2020/10/23.
// Copyright © 2020 zhaochen. All rights reserved.
//
#import "YFTestCell.h"
#import "Masonry.h"
@implementation YFTestCell
{
UILabel *_titleLab;
UIScrollView *_containerScrollView;
NSMutableArray<UILabel *> *_labArr;
}
+(YFTestCell *)cellWithTableView:(UITableView *)tableView {
static NSString *identifier = @"YFTestCell";
YFTestCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[YFTestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setUpView2];
}
return self;
}
- (void)setUpView{
_titleLab = [[UILabel alloc] init];
_titleLab.textColor = [UIColor blackColor];
_titleLab.font = [UIFont systemFontOfSize:18];
_titleLab.numberOfLines = 0;
[self.contentView addSubview:_titleLab];
[_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).offset(10);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.top.mas_equalTo(self.contentView.mas_top).offset(20);
make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-20);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 5);
}];
_containerScrollView = [[UIScrollView alloc] init];
_containerScrollView.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:_containerScrollView];
[_containerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_titleLab.mas_right);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.top.mas_equalTo(self.contentView.mas_top);
make.bottom.mas_equalTo(self.contentView.mas_bottom);
make.right.mas_equalTo(self.contentView.mas_right);
}];
_labArr = [NSMutableArray array];
NSInteger totalCount = 20;
for (NSInteger i = 0; i < totalCount; i++) {
UILabel *valueLab = [[UILabel alloc] init];
valueLab.textColor = [UIColor blueColor];
valueLab.font = [UIFont systemFontOfSize:14];
valueLab.numberOfLines = 0;
[_containerScrollView addSubview:valueLab];
[_labArr addObject:valueLab];
[valueLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_containerScrollView.mas_left).offset(i * ([UIScreen mainScreen].bounds.size.width / 5 + 10));
make.centerY.mas_equalTo(_containerScrollView.mas_centerY);
make.top.mas_equalTo(_containerScrollView.mas_top).offset(20);
make.bottom.mas_equalTo(_containerScrollView.mas_bottom).offset(-20);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 5);
}];
if (i == totalCount - 1) {
//这种做法,_containerScrollView的宽度依赖于最后一个子控件的位置,相当于自己很大,所以能滚动,setUpView2方法是_containerScrollView上的子控件bgView很大,导致_containerScrollView可以滚动
[_containerScrollView mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(valueLab.mas_right).offset(10);
}];
}
}
}
- (void)setUpView2{
_titleLab = [[UILabel alloc] init];
_titleLab.textColor = [UIColor blackColor];
_titleLab.font = [UIFont systemFontOfSize:18];
_titleLab.numberOfLines = 0;
[self.contentView addSubview:_titleLab];
[_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).offset(10);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.top.mas_equalTo(self.contentView.mas_top).offset(20);
make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-20);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 5);
}];
_containerScrollView = [[UIScrollView alloc] init];
_containerScrollView.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:_containerScrollView];
[_containerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_titleLab.mas_right);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.top.mas_equalTo(self.contentView.mas_top);
make.bottom.mas_equalTo(self.contentView.mas_bottom);
make.right.mas_equalTo(self.contentView.mas_right);
}];
UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor redColor];
[_containerScrollView addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_containerScrollView);
make.height.equalTo(_containerScrollView);
//此处不能限制make.width,不然bgView的宽度固定后_containerScrollView就无法滚动,
//bgView宽度应该依赖于添加到bgView上的最后一个子控件,bgView的撑开也就是_containerScrollView的滚动范围
}];
_labArr = [NSMutableArray array];
NSInteger totalCount = 20;
for (NSInteger i = 0; i < totalCount; i++) {
UILabel *valueLab = [[UILabel alloc] init];
valueLab.textColor = [UIColor blueColor];
valueLab.font = [UIFont systemFontOfSize:14];
valueLab.numberOfLines = 0;
[bgView addSubview:valueLab];
[_labArr addObject:valueLab];
[valueLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(bgView.mas_left).offset(i * ([UIScreen mainScreen].bounds.size.width / 5 + 10));
make.centerY.mas_equalTo(bgView.mas_centerY);
make.top.mas_equalTo(bgView.mas_top).offset(20);
make.bottom.mas_equalTo(bgView.mas_bottom).offset(-20);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 5);
}];
if (i == totalCount - 1) {
[bgView mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(valueLab.mas_right).offset(10);
}];
}
}
}
- (void)setDic:(NSDictionary *)dic {
_dic = dic;
_titleLab.text = @"测试还是回到哈哈哈";
[_labArr enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (idx == 1 || idx == 3) {
obj.text = [NSString stringWithFormat:@"恩恩嫩嫩额嗯%ld",idx];
}else {
obj.text = [NSString stringWithFormat:@"哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈hahha哈哈哈哈哈多喝点哈哈哈多好多好哈哈都会好的换货单号%ld",idx];
}
}];
}
@end