Masonry小技巧之UIScrollView自适应

以往我们使用UIScrollView的时候需要设置它的frame和contentSize.其中frame是UIScrollView的窗口大小,contentSize是内容大小,也就是滚动的范围,当然contentSize小于frame.size的话是没法滚动的。

今天带给大家分享一个使用masonry来使contentSize自适应的小技巧

一、宽高都适应

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-200);
        make.top.mas_equalTo(200);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor yellowColor];
    [scrollView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
    }];
    
    UIView *view0 = [[UIView alloc] init];
    view0.backgroundColor = [UIColor purpleColor];
    [containerView addSubview:view0];
    [view0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.mas_equalTo(0);
        make.width.mas_equalTo(600);
        make.height.mas_equalTo(600);
    }];
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view0);
        make.bottom.equalTo(view0);
    }];
    
}
效果
宽高自适应

二、宽适应

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-200);
        make.top.mas_equalTo(200);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor yellowColor];
    [scrollView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.height.equalTo(scrollView);//这个不能省略
    }];
    
    UIView *view0 = [[UIView alloc] init];
    view0.backgroundColor = [UIColor purpleColor];
    [containerView addSubview:view0];
    [view0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.mas_equalTo(0);
        make.width.mas_equalTo(600);
    }];
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view0);
    }];
    
}
效果
宽适应

三、高适应

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-200);
        make.top.mas_equalTo(200);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor yellowColor];
    [scrollView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);//这个不能省略
    }];
    
    UIView *view0 = [[UIView alloc] init];
    view0.backgroundColor = [UIColor purpleColor];
    [containerView addSubview:view0];
    [view0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo(600);
    }];
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(view0);
    }];
    
}
效果
高适应

四、还可以这样

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-200);
        make.top.mas_equalTo(200);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor yellowColor];
    [scrollView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.height.equalTo(scrollView);
    }];
    
    UIView *view0 = [[UIView alloc] init];
    view0.backgroundColor = [UIColor purpleColor];
    [containerView addSubview:view0];
    [view0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.mas_equalTo(0);
        make.width.mas_equalTo(600);
    }];
    
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor orangeColor];
    [containerView addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(view0.mas_right);
        make.width.mas_equalTo(600);
    }];
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view1);
    }];
    
}
效果
多个view
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容