//
// ViewController.m
// 1_AutoLayout(code)
//
// Created by mac on 16/7/13.
// Copyright © 2016年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//1.调用创建视图的方法
[selfcreateView];
}
//创建视图的方法
- (void) createView {
//1.创建视图1(蓝色)
//a) 创建
UIView *cyanView = [[UIViewalloc]init];
//b) 背景颜色
cyanView.backgroundColor = [UIColorcyanColor];
//c) 取消默认的Autoresizing自动添加约束,避免冲突
cyanView.translatesAutoresizingMaskIntoConstraints =NO;
//d) 添加到视图
[self.viewaddSubview:cyanView];
//2.创建视图2(绿色)
//a) 创建
UIView *greenView = [[UIViewalloc]init];
//b) 背景颜色
greenView.backgroundColor = [UIColorgreenColor];
//c) 取消默认的Autoresizing自动添加约束,避免冲突
greenView.translatesAutoresizingMaskIntoConstraints =NO;
//d) 添加到视图
[self.viewaddSubview:greenView];
//2.创建视图3(红色)
//a) 创建
UIView *magentaView = [[UIViewalloc]init];
//b) 背景颜色
magentaView.backgroundColor = [UIColormagentaColor];
//c) 取消默认的Autoresizing自动添加约束,避免冲突
magentaView.translatesAutoresizingMaskIntoConstraints =NO;
//d) 添加到视图
[self.viewaddSubview:magentaView];
//2.编写VFL语句
//a)cyanView和greenView的水平约束
NSString *hVFL =@"H:|-Space-[cyanView(==greenView)]-Between-[greenView]-Space-|";
//b) magentaView的水平约束
NSString *hBottom =@"H:|-Space-[magentaView]-Space-|";
//c) cyanView和magentaView的垂直约束
NSString *vLeft =@"V:|-Space-[cyanView(==magentaView)]-Between-[magentaView]-Space-|";
//d) greenView和magentaView的垂直约束
NSString *vRight =@"V:|-Space-[greenView]-Between-[magentaView]-Space-|";
//e) 将约束条件和视图,放入字典中
NSDictionary *metrics =@{@"Space":@20,@"Between":@20};
NSDictionary *views1 =NSDictionaryOfVariableBindings(cyanView,greenView);
NSDictionary *views2 =NSDictionaryOfVariableBindings(magentaView);
NSDictionary *views3 =NSDictionaryOfVariableBindings(cyanView,magentaView);
NSDictionary *views4 =NSDictionaryOfVariableBindings(greenView,magentaView);
//3.创建约束
//a) 两个水平约束
NSArray *hVFLContraints = [NSLayoutConstraint constraintsWithVisualFormat:hVFLoptions:NSLayoutFormatDirectionLeadingToTrailingmetrics:metricsviews:views1];
NSArray *hBottomContraints = [NSLayoutConstraint constraintsWithVisualFormat:hBottomoptions:NSLayoutFormatDirectionLeadingToTrailingmetrics:metricsviews:views2];
//b) 两个垂直约束
NSArray *vLeftContraints = [NSLayoutConstraint constraintsWithVisualFormat:vLeftoptions:NSLayoutFormatDirectionLeadingToTrailingmetrics:metricsviews:views3];
NSArray *vRightContraints = [NSLayoutConstraint constraintsWithVisualFormat:vRightoptions:NSLayoutFormatDirectionLeadingToTrailingmetrics:metricsviews:views4];
//4.将约束全部添加到父视图
[self.viewaddConstraints:hVFLContraints];
[self.viewaddConstraints:hBottomContraints];
[self.viewaddConstraints:vLeftContraints];
[self.viewaddConstraints:vRightContraints];
}
@end