//这个地方不要定义Frame,下面的Visual Format才是定义Frame相关的地方
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
//参照Apple Doc 这个地方设置NO,是为了不让Constraint与View本身的autoresize冲突
[redView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:redView];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[blueView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:blueView];
//绑定两个View相关
NSDictionary *views = NSDictionaryOfVariableBindings(redView, blueView);
//此处的constraint是为了定义Frame水平方向相关(x, width)
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=10)-[redView(200)]"
options:0
metrics:nil
views:views]];
//此处的constraint是为了定义Frame竖直方向相关(y, height)
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=240)-[redView(100)]"
options:0
metrics:nil
views:views]];
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:[blueView(==redView)]"
options:0
metrics:nil
views:views]];
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(40)-[blueView(==redView)]"
options:0
metrics:nil
views:views]];
//此处的constraint是为了描述以上两个view的关系
[self.view addConstraint: [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:redView
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0]];
以上.
关于Method和Macro不提供解释,请参照Apple Doc.