Autolayout(VFL)
1、 NSLayoutConstraint
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
format:此参数为你的vfl语句,比如:@"H:|-[button]-|"
opts:枚举参数,默认写0
metrics:这里是一个字典,当在format中使用了动态数据比如上现这句:@"H:|-[button(==width)]-|",表示这个button的宽度为width,那么这个参数去哪里找呢?就是在这个字典里面找到key对就的值,如果没有找到这个值,app会crash
views:是传所有你在vfl中使用到的view, NSDictionaryOfVariableBindings(button).如果你使用到了多个view,就可以这样 NSDictionaryOfVariableBindings(button,button1,button3...),这个名字也要跟参数 format中的一一对应,缺一不可.
2、 添加约束
UIView
- (void)addConstraints:(NSArray *)constraints;
3、 简单的使用
UIView *view = [[UIView alloc] init];//如果使用了vfl,可以忽略视图的frame,避免与后面的约束条件冲突
view.backgroundColor = [UIColor grayColor]; view.translatesAutoresizingMaskIntoConstraints = NO;//这一句必须有,如果要实现自动布局,必须将此属性设置为no,否则约束不生效
[self.view addSubview:view];
NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];//设置约束条件,代表距离父View左右各20像素
NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[view(==200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];//设置约束,表示距离顶部40像素,view的高度为200
[self.view addConstraints:arr1];//添加约束
[self.view addConstraints:arr];
4、 vfl还可以针对两个平级的view进行设置
NSArray *arr3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-30-[view1]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view,view1)];//表示View1的纵坐标比view 的多30像素,此时最后一个参数views,需要把涉及的两个view都传进去
根据屏幕尺寸设定缩放
1、 以iPhone5待屏幕为标准,根据6、6p相对于5的比例进行缩放
2、 获取屏幕宽度
#define WIDTH [UIScreen mainScreen].bounds.size.width
3、 获取比例
#define SCALE (WIDTH/320.0)
4、 自定义rect宏
#define CGRect1(x,y,width,heigth) CGRectMake(x*SCALE, y*SCALE, width*SCALE, heigth*SCALE)
5、 使用
UIView *view = [[UIView alloc] initWithFrame:CGRect1(10, 10, 300, 548)];
使用举例
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self fun5];
}
-(void)fun1{
//注意使用VFL,不用设置视图的frame
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor brownColor];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;//注意,要想使约束条件起作用,必须将此属性设置为no
/*
VFL相关的标识符
H:代表水平方向
V:代表垂直方向
|代表父视图
- 代表距离 例(H:|-20-)表示距离父视图左侧20像素
[]代表子视图相关属性
*/
//对子视图View进行约束设置,format表示要设置的约束语句,在本句中意思为:view距离父视图左侧30像素,距离父视图右侧20像素(如果没设置具体的数字,且写了-)
//options代表对其方式,默认写0就行
//metrics 这里是一个字典,当在format中使用了动态数据比如上现这句:@"H:|-[button(==width)]-|",表示这个button的宽度为width,那么这个参数去哪里找呢?就是在这个字典里面找到key对就的值,如果没有找到这个值,app会crash
//views 代表约束条件中所涉及的所有视图(父视图除外)
NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
[self.view addConstraints:arr];
[self.view addConstraints:arr1];
}
-(void)fun2{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
//[view(>=200)] 表示给view设置尺寸,此时小括号里的关系运算符只能是 == <= >=,注意,改尺寸的值不要与约束冲突,==:约束可以不写,简写成H:[view(==200)] >=:当左右距离只设置了一个或者都未设置,视图的尺寸为最小值,如果两个都设置时,view 的尺寸为屏幕宽度减去左右距离,此时要保证减后的值>=尺寸最小值
NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view(>=200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
[self.view addConstraints:arr1];
[self.view addConstraints:arr];
}
-(void)fun3{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
CGFloat width = 200;
//当我们在设置约束条件时,距离父视图上下左右的像素值以及view的尺寸不是固定的数字(用某个字符串代替),这时我们需要给metrics设置一个字典,该字典的key必须是约束语句中使用的字符串,对应的value可以自行设置
NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(==width)]" options:0 metrics:@{@"width":[NSNumber numberWithFloat:width],@"left":@30} views:NSDictionaryOfVariableBindings(view)];
NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
[self.view addConstraints:arr1];
[self.view addConstraints:arr];
}
-(void)fun4{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
CGFloat width = 200;
//当我们在设置约束条件时,距离父视图上下左右的像素值以及view的尺寸不是固定的数字(用某个字符串代替),这时我们需要给metrics设置一个字典,该字典的key必须是约束语句中使用的字符串,对应的value可以自行设置
NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(==width)]" options:0 metrics:@{@"width":[NSNumber numberWithFloat:width],@"left":@30} views:NSDictionaryOfVariableBindings(view)];
NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
[self.view addConstraints:arr1];
[self.view addConstraints:arr];
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor purpleColor];
[self.view addSubview:view1];
view1.translatesAutoresizingMaskIntoConstraints = NO;
//[view1(view)] 代表view1和view某个方向尺寸相同
NSArray *arr2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view1(view)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view1,view)];
//V:[view]-[view1]-20-|:子视图view1垂直方向距离平级子视图view 8像素(默认)
NSArray *arr3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-[view1]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view,view1)];
[self.view addConstraints:arr2];
[self.view addConstraints:arr3];
}
//用VFL实现label自适应
-(void)fun5{
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor grayColor];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByCharWrapping;
label.text = @"hiwehfdiuwehdiowqhdoiwehjdoiuwehdiouwhediuwheioudhewiuodhwehoihiuhiuhiuhiluhiuhiuhiuhihuhuuhhuh";
[self.view addSubview:label];
label.translatesAutoresizingMaskIntoConstraints = NO;
NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)];
NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)];
[self.view addConstraints:arr];
[self.view addConstraints:arr1];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end