VFL语言

VFL语言

  • VFL即Visual Format Language,可视化格式语言
  • NSDictionaryOfVariableBindings(testViewA, testViewB):此为一个宏,直接调用,会返回一个key值即为对象名称,键值为对象的字典,如同以下方法一一模一样的字典
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *testViewA = [[UIView alloc] init];
    testViewA.backgroundColor = [UIColor blueColor];
    //将testViewA的AutoresizingMask自动转化成约束的属性关闭
    testViewA.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:testViewA];
    
    UIView *testViewB = [[UIView alloc] init];
    testViewB.backgroundColor = [UIColor redColor];
    //将testViewB的AutoresizingMask自动转化成约束的属性关闭
    testViewB.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:testViewB];
    
    //创建参数views字典方式一
    NSDictionary *viewDict1 = @{
                          @"testViewA":testViewA,
                          @"testViewB":testViewB
                          };
    //创建参数metrics字典
    NSDictionary *constant = @{
                               @"space":@30
                               };
    //创建水平方向的约束
    NSString *hVFL = @"H:|-space-[testViewA]-space-[testViewB(==testViewA)]-space-|";
    NSArray *hConstrains = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom  metrics:constant views:viewDict1];
    [self.view addConstraints:hConstrains];
    
    //创建参数views字典方式二
    NSDictionary *viewDict2 = NSDictionaryOfVariableBindings(testViewA, testViewB);
    //创建垂直方向的约束
    NSString *vVFL = @"V:[testViewA(50)]-space-|";
    NSArray *vConstrains = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:kNilOptions metrics:constant views:viewDict2];
    [self.view addConstraints:vConstrains];


}
@end
原文地址:https://www.cnblogs.com/igeniuswwh/p/6209139.html