UIScrollView的不等高cell效果

250 阅读1分钟
#import "ViewController.h"
#import <Masonry/Masonry.h>

@interface ViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.scrollView];
    [self createLabels];
}

- (void)createLabels {
    UILabel *lastLabel = nil;
    for (NSInteger i = 0; i < 20; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.layer.borderColor = [UIColor greenColor].CGColor;
        label.layer.borderWidth = 2.0;
        label.text = [self randomText];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [self randomColor];
        [self.scrollView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(self.view.mas_right).mas_offset(-15);
            if (lastLabel) {
                make.top.mas_equalTo(lastLabel.mas_bottom).mas_offset(20);
            } else {
                make.top.mas_equalTo(self.scrollView.mas_top).mas_offset(20);
            }
        }];
        lastLabel = label;
    }
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
        make.bottom.mas_equalTo(lastLabel.mas_bottom).mas_offset(20);// 让scrollview的contentSize随着内容的增多而变化
    }];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.pagingEnabled = NO;
        _scrollView.backgroundColor = [UIColor lightGrayColor];
    }
    return _scrollView;
}

- (UIColor *)randomColor {
    CGFloat hue = (arc4random() % 256 / 256.0);
    CGFloat saturation = (arc4random() % 128 / 256.0) + 0.5;
    CGFloat brightness = (arc4random() % 128 / 256.0) + 0.5;
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

- (NSString *)randomText {
    CGFloat count = arc4random() % 50 + 5;
    NSMutableString *str = [[NSMutableString alloc] init];
    for (NSInteger i = 0; i < count; i++) {
        [str appendString:@"测试数据很长,"];
    }
    return str;
}

@end

#import "ViewController.h"
#import <Masonry/Masonry.h>

@interface ViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *containerView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    [self.scrollView addSubview:self.containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.scrollView).insets(UIEdgeInsetsMake(0, 0, 0, 0 ));
        //对于scrollView, 最好把要把想放在scrollView内部的控件, 放在一个容器的View里, 这个View的作用就是衔接内外, 因为scrollView除了自身的frame, 还有containSize, 需要注意的是, 如果仅上面这句话, 会横向撑开, 要实现效果, 一定要一个具体的宽度值, 可以注释掉下面这句话看一下效果, 另外如果不加这个containerView, 那么内部的控件, 就一定要有具体的宽度值
        make.width.mas_equalTo(self.scrollView);
    }];
    
    UIView *lastView; //定义一个指向上一个View的指针, 以便循环中获取上一个View, 相对这个View布局
    for (NSInteger i = 0; i < 20; i++) {
        UILabel *contentLabel = [[UILabel alloc] init];
        contentLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
        contentLabel.numberOfLines = 0;
        contentLabel.text = [self getRandomLengthStr];
        [self.containerView addSubview:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.containerView).mas_offset(20);
            make.right.mas_equalTo(self.containerView).mas_offset(-20);
            //第一个label时顶部要针对containerView 布局, 之后的label都要针对上一个label
            if (i == 0) {
                make.top.mas_equalTo(self.containerView).mas_offset(20);
            } else {
                make.top.mas_equalTo(lastView.mas_bottom).mas_offset(10);
            }
            //最后一个label, 要记得对containerView底部约束, 这样才会"撑满", 或者放在循环结束, 对最后一个lastView约束也可以
            if (i == 19) {
                make.bottom.mas_equalTo(self.containerView).mas_offset(-20);
            }
        }];
        lastView = contentLabel;
    }
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.3];
    }
    return _scrollView;
}

- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [[UIView alloc] init];
    }
    return _containerView;
}

- (NSString *)getRandomLengthStr {
    NSMutableString *str = [NSMutableString string];
    for (NSInteger i = 0; i < (arc4random() % 50) + 10; i++) {
        [str appendString:@"测试一下 "];
    }
    return [str copy];
}

@end