#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor yellowColor];
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
[self.view addSubview:view1];
view.translatesAutoresizingMaskIntoConstraints = NO;
view1.translatesAutoresizingMaskIntoConstraints = NO;
//VFL解释
//'V:' 表示当前语句为垂直布局,不可省略
//'H:' 表示当前语句水平布局,可以省略
//'|' :表示superView
//'-20-': 表示Spacing, 间距
//'[_label1]': 用于VFL布局的控件名称
//'(100)': 控件尺寸
//@"V:|-20-[_label1]-30-|": 为_label1设置一个垂直布局,其中_label1距离父视图'|'上部的距离为20('-20-'), 并且_label2的右边与父视图的下部的距离
//@"H:|-20-[_label1(100)]-10-[_label2]-10-|", 设置一个水平布局
NSString *viewHStr = @"H:|-20-[view(50)]";
NSString *viewVStr = @"V:|-20-[view(20)]";
NSString *view1HStr = @"H:[view]-20-[view1(100)]";
NSString *view1VStr = @"V:|-20-[view1(50)]";
//将控件对象转换为可以用于VFL的字典,用逗号分隔多个控件
NSDictionary *dic = NSDictionaryOfVariableBindings(view,view1);
//将VFL(Visual Format Language)转换为布局对象
NSArray *viewH = [NSLayoutConstraint constraintsWithVisualFormat:viewHStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
NSArray *viewV = [NSLayoutConstraint constraintsWithVisualFormat:viewVStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
NSArray *view1H = [NSLayoutConstraint constraintsWithVisualFormat:view1HStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
NSArray *view1V = [NSLayoutConstraint constraintsWithVisualFormat:view1VStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
// NSArray *arr = [viewH arrayByAddingObjectsFromArray:<#(NSArray *)#>];
[self.view addConstraints:viewH];
[self.view addConstraints:viewV];
[self.view addConstraints:view1H];
[self.view addConstraints:view1V];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor yellowColor];
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
[self.view addSubview:view1];
view.translatesAutoresizingMaskIntoConstraints = NO;
view1.translatesAutoresizingMaskIntoConstraints = NO;
//VFL解释
//'V:' 表示当前语句为垂直布局,不可省略
//'H:' 表示当前语句水平布局,可以省略
//'|' :表示superView
//'-20-': 表示Spacing, 间距
//'[_label1]': 用于VFL布局的控件名称
//'(100)': 控件尺寸
//@"V:|-20-[_label1]-30-|": 为_label1设置一个垂直布局,其中_label1距离父视图'|'上部的距离为20('-20-'), 并且_label2的右边与父视图的下部的距离
//@"H:|-20-[_label1(100)]-10-[_label2]-10-|", 设置一个水平布局
NSString *viewHStr = @"H:|-20-[view(50)]";
NSString *viewVStr = @"V:|-20-[view(20)]";
NSString *view1HStr = @"H:[view]-20-[view1(100)]";
NSString *view1VStr = @"V:|-20-[view1(50)]";
//将控件对象转换为可以用于VFL的字典,用逗号分隔多个控件
NSDictionary *dic = NSDictionaryOfVariableBindings(view,view1);
//将VFL(Visual Format Language)转换为布局对象
NSArray *viewH = [NSLayoutConstraint constraintsWithVisualFormat:viewHStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
NSArray *viewV = [NSLayoutConstraint constraintsWithVisualFormat:viewVStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
NSArray *view1H = [NSLayoutConstraint constraintsWithVisualFormat:view1HStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
NSArray *view1V = [NSLayoutConstraint constraintsWithVisualFormat:view1VStr options:NSLayoutFormatAlignAllTop metrics:nil views:dic];
// NSArray *arr = [viewH arrayByAddingObjectsFromArray:<#(NSArray *)#>];
[self.view addConstraints:viewH];
[self.view addConstraints:viewV];
[self.view addConstraints:view1H];
[self.view addConstraints:view1V];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end