Masonry小技巧之UIScrollView自适应

作者: 口子窖 | 来源:发表于2018-09-21 20:32 被阅读78次

    以往我们使用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

    相关文章

      网友评论

        本文标题:Masonry小技巧之UIScrollView自适应

        本文链接:https://www.haomeiwen.com/subject/kmyznftx.html