我们平时在使用Scrollview 的频率还是挺高的,有时候我们通过frame 来计算Scrollview 的 contentSize,简单的页面还是比较容易计算的,要是里面有很多子控件,首先设置frame 就是一个很大的工作量了,然后在累积各个子控件的高度和间隙的高度,那简直就是折磨啊。iOS 6之后引入了自动布局,大大减轻了负担。接下来总结一下,自己在使用 Scrollview 约束布局的一些坑。(主要两种方式:xib 和 代码)
一、xib 布局 --简单粗暴
前面写了一篇关于Scrollview快速布局不报错的小技巧,但是有坑(不推荐使用),你们看看就好了。
原理:UIScrollView 约束计算 contentSize, 其实就是通过给 Ta 添加一个 UIView 作为 TA 的 contentView (子控件的容器),contentView 的 height == scrollview.contentSize.height (contentView 的高度就是,滚动视图的滚动范围的height)
步骤1: 先给 scrollView 添加约束,你会发现多加了那个 ceter X 约束之后还是报缺少约束,此时先不要管,等所有子控件约束设置好都就不会报错了
步骤2: 给子视图添加约束
步骤3: 注意最后一个子视图的约束,关系到 contentSize 的值哦
二、代码约束
直接上代码
//
// ViewController.m
// Tm-test
//
// Created by Mac on 2018/7/19.
// Copyright © 2018年 Mac. All rights reserved.
//
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;//子视图容器
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIView *yellowView;
@property (nonatomic, strong) UIView *purpleView;
@property (nonatomic, strong) UIView *blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubView];
[self layoutAllSubView];
}
- (void)addSubView
{
[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.contentView];
[self.contentView addSubview:self.redView];
[self.contentView addSubview:self.yellowView];
[self.contentView addSubview:self.purpleView];
[self.contentView addSubview:self.blueView];
}
//layoutSubView原本方法与系统方法太像导致某些误解,现在更正
- (void)layoutAllSubView
{
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.right.mas_equalTo(0);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
## 法1: 代码可以 《|》 xib 会报约束不够
// make.top.bottom.mas_equalTo(0);
// make.width.equalTo(self.scrollView);
## 法2:
make.top.left.bottom.right.mas_equalTo(0);
make.centerX.equalTo(self.scrollView);##//不能加 centerY 不然算不准,xib 可以加(不加会报缺少约束,但是设置子控件约束完之后就不报错了);原因我猜是:它都是可以向四方延伸,它不能确定
}];
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(30);
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.height.mas_equalTo(300);
}];
[self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.redView.mas_bottom).offset(30);
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.height.mas_equalTo(400);
}];
[self.purpleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.yellowView.mas_bottom).offset(30);
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.height.mas_equalTo(500);
}];
##//注意⚠️:最后一个 view 要多设置一个底部约束,这个约束是,最后一个 view 和滚动范围最底下的间距
[self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.purpleView.mas_bottom).offset(30);
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.height.mas_equalTo(600);
## //注意⚠️ : 最后一个 view 和滚动范围最底下的间距
make.bottom.mas_equalTo(-30);
}];
}
#pragma mark - getter
- (UIScrollView *)scrollView
{
if (_scrollView == nil) {
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor grayColor];
_scrollView = scrollView;
}
return _scrollView;
}
- (UIView *)contentView
{
if (_contentView == nil) {
UIView *av = [[UIView alloc] init];
_contentView = av;
}
return _contentView;
}
- (UIView *)redView
{
if (_redView == nil) {
UIView *av = [[UIView alloc] init];
av.backgroundColor = [UIColor redColor];
_redView = av;
}
return _redView;
}
- (UIView *)yellowView
{
if (_yellowView == nil) {
UIView *av = [[UIView alloc] init];
av.backgroundColor = [UIColor yellowColor];
_yellowView = av;
}
return _yellowView;
}
- (UIView *)purpleView
{
if (_purpleView == nil) {
UIView *av = [[UIView alloc] init];
av.backgroundColor = [UIColor purpleColor];
_purpleView = av;
}
return _purpleView;
}
- (UIView *)blueView
{
if (_blueView == nil) {
UIView *av = [[UIView alloc] init];
av.backgroundColor = [UIColor blueColor];
_blueView = av;
}
return _blueView;
}
@end