iOS VFL自动布局的小秘密

    self.redView = [[UIView alloc]init];
    self.redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.redView];
    self.blueView = [[UIView alloc]init];
    self.blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.blueView];
    self.redView.translatesAutoresizingMaskIntoConstraints = NO;
    self.blueView.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = NSDictionaryOfVariableBindings(self.redView, self.blueView);
    //与其相对应的是@{@"self.redView": self.redView, @"self.blueView": self.blueView}
    views = @{@"redView": self.redView, @"blueView": self.blueView};
    //这句代码探明了、前面的key不是一定要与后面的值完全一样、只要这个key与控件绑定了就可以、与下面的这个定义相同、只要能传值就可以
    NSDictionary *spaceMetrics = @{@"space": @30};
    spaceMetrics = @{@"space": @"30"};
    //所以这样写也是可以识别为30
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-space-[blueView]-space-[redView(==blueView)]-space-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:spaceMetrics views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[blueView(50)]-space-|" options:0 metrics:spaceMetrics views:views]];

充分说明了VFL还是很灵活的、系统的方法也挺好用的、只要把原来的像素布局稍微思维稍微改变一下就可以很快适应自动布局、比如我自个儿写的这些、我认为我最需要这几个方法

CommonAutoLayout

你可能感兴趣的:(iOS)