自动布局VisualFormat

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值