UIScrollView+Masonry 自适应宽高

宽高自适应

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    
    UIView * containView = [[UIView alloc] init];
    [containView addSubview:whiteView];
    
    [self.scrollView addSubview:containView];
    [self.view addSubview:self.scrollView];

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
    }];
    
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(0);
        make.right.bottom.mas_equalTo(1000);
    }];
    
    //这里会修改contentSize
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(whiteView.mas_right);
        make.bottom.equalTo(whiteView.mas_bottom);
    }];
}
复制代码

高度自适应

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    
    UIView * containView = [[UIView alloc] init];
    [containView addSubview:whiteView];
    
    [self.scrollView addSubview:containView];
    [self.view addSubview:self.scrollView];

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.mas_equalTo(1000);
    }];
    
    //高度自适应 这里会修改contentSize
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(whiteView.mas_bottom);
    }];
}
复制代码

宽度自适应

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    
    UIView * containView = [[UIView alloc] init];
    [containView addSubview:whiteView];
    
    [self.scrollView addSubview:containView];
    [self.view addSubview:self.scrollView];

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView);
    }];
    
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.offset(0);
        make.right.mas_equalTo(1000);
    }];
    
    //宽度自适应 这里会修改contentSize
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(whiteView.mas_right);
    }];
}
复制代码

你可能感兴趣的:(UIScrollView+Masonry 自适应宽高)