//
// ViewController.m
// 原始 autolayout 使用
//
// Created by hzc on 2017/10/9.
// Copyright © 2017年 hzc. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIView *blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView * bgScrollView = [[UIScrollView alloc] init];
bgScrollView.backgroundColor = [UIColor yellowColor];
bgScrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bgScrollView];
_bgScrollView = bgScrollView;
UIView *contentView = [[UIView alloc] init];
contentView.backgroundColor = [UIColor cyanColor];
contentView.translatesAutoresizingMaskIntoConstraints = NO;
[_bgScrollView addSubview:contentView];
_contentView = contentView;
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
view1.translatesAutoresizingMaskIntoConstraints = NO;
[_contentView addSubview:view1];
_redView = view1;
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor blueColor];
view2.translatesAutoresizingMaskIntoConstraints = NO;
[_contentView addSubview:view2];
_blueView = view2;
[self setLayout];
}
- (void)setLayout {
// 设置_bgScrollView的约束
// 上
NSLayoutConstraint *topCons = [NSLayoutConstraint constraintWithItem:_bgScrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0];
// 左
NSLayoutConstraint *leftCons = [NSLayoutConstraint constraintWithItem:_bgScrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
// 右
NSLayoutConstraint *rightCons = [NSLayoutConstraint constraintWithItem:_bgScrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
// 下
NSLayoutConstraint *bottomCons = [NSLayoutConstraint constraintWithItem:_bgScrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-60];
[self.view addConstraints:@[topCons,leftCons,rightCons,bottomCons]];
// 设置 contentView的约束
// 上
NSLayoutConstraint *topConsC = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_bgScrollView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
// 左
NSLayoutConstraint *leftConsC = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_bgScrollView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
// 右
NSLayoutConstraint *rightConsC = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_bgScrollView attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
// 宽 (给 contentView 的宽度一个固定的值)
NSLayoutConstraint *widthConsC = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
// 下
NSLayoutConstraint *bottomConsC = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_bgScrollView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
[self.view addConstraints:@[widthConsC]];
[_bgScrollView addConstraints:@[topConsC,leftConsC,rightConsC,bottomConsC]];
// 设置redView和blueView的约束
// 上
NSLayoutConstraint *topConsR = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeTop multiplier:1 constant:20];
// 左
NSLayoutConstraint *leftConsR = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:10];
// 右
NSLayoutConstraint *rightConsR = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10];
// 下
NSLayoutConstraint * bottomConsR = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_blueView attribute:NSLayoutAttributeTop multiplier:1 constant:-20];
// 高
NSLayoutConstraint *heightConsR = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:200];
[_redView addConstraints:@[heightConsR]];
[_contentView addConstraints:@[topConsR,leftConsR,rightConsR,bottomConsR]];
// 上 blueView 的上边约束这里可以不用再设置了,因为 redView 的下边约束和这一条约束其实是同一个
// NSLayoutConstraint *topConsB = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_redView attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
// 左
NSLayoutConstraint *leftConsB = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:10];
// 右
NSLayoutConstraint *rightConsB = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10];
// 下
NSLayoutConstraint * bottomConsB = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:-10];
// 高
NSLayoutConstraint *heightConsB = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:700];
[_blueView addConstraints:@[heightConsB]];
[_contentView addConstraints:@[leftConsB,rightConsB,bottomConsB]];
}
@end
效果图:
1.gif
网友评论