Skip to content

Instantly share code, notes, and snippets.

@adin283
Created December 22, 2015 02:26
Show Gist options
  • Select an option

  • Save adin283/a6e3be8a14ae2bf96ef3 to your computer and use it in GitHub Desktop.

Select an option

Save adin283/a6e3be8a14ae2bf96ef3 to your computer and use it in GitHub Desktop.

Revisions

  1. adin283 created this gist Dec 22, 2015.
    93 changes: 93 additions & 0 deletions UIScrollViewAutoLayoutDemo.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    //
    // ZZZViewController.m
    // KevinDemo
    //
    // Created by Kevin on 15/12/22.
    // Copyright © 2015年 KevinLab. All rights reserved.
    //

    #import "ZZZViewController.h"

    @interface ZZZViewController ()

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

    @end

    @implementation ZZZViewController

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.view1];
    [self.scrollView addSubview:self.view2];

    [self configureConstraints];
    }

    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

    #pragma mark - Getters

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

    - (UIView *)view1
    {
    if (!_view1) {
    _view1 = [[UIView alloc] init];
    _view1.backgroundColor = [UIColor colorWithHex:0x0a88e3];
    }
    return _view1;
    }

    - (UIView *)view2
    {
    if (!_view2) {
    _view2 = [[UIView alloc] init];
    _view2.backgroundColor = [UIColor colorWithHex:0xCD7320];
    }
    return _view2;
    }

    - (void)configureConstraints
    {
    [self.scrollView autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.scrollView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [self.scrollView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.scrollView autoPinEdgeToSuperviewEdge:ALEdgeRight];

    [self.view1 autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.view1 autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.view1 autoPinEdgeToSuperviewEdge:ALEdgeRight];
    // 确定 contentSize 重要的约束
    [self.view1 autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
    [self.view1 autoSetDimension:ALDimensionHeight toSize:400];

    [self.view2 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.view1];
    [self.view2 autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.view2 autoPinEdgeToSuperviewEdge:ALEdgeRight];
    // 确定 contentSize 重要的约束
    [self.view2 autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
    [self.view2 autoSetDimension:ALDimensionHeight toSize:800];
    // 确定 contentSize 重要的约束
    [self.view2 autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    }

    @end