// Do not define the Frame in this place. The following Visual Format defines the Frame.
UIView * redView = [[UIView alloc] init];
RedView. backgroundColor = [UIColor redColor];
// Set NO according to the Apple Doc to prevent the autoresize conflict between the Constraint and View.
[RedView setTranslatesAutoresizingMaskIntoConstraints: NO];
[Self. view addSubview: redView];
UIView * blueView = [[UIView alloc] init];
BlueView. backgroundColor = [UIColor blueColor];
[BlueView setTranslatesAutoresizingMaskIntoConstraints: NO];
[Self. view addSubview: blueView];
// Bind two views
NSDictionary * views = NSDictionaryOfVariableBindings (redView, blueView );
// The constraint is used to define the horizontal direction correlation (x, width) of the Frame)
[Self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-(> = 10)-[redView (200)]"
Options: 0
Metrics: nil
Views: views];
// The constraint is used to define the vertical correlation (y, height) of the Frame)
[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];
// The constraint here is used to describe the relationship between the two views.
[Self. view addConstraint: [NSLayoutConstraint constraintWithItem: blueView
Attribute: NSLayoutAttributeLeft
RelatedBy: NSLayoutRelationEqual
ToItem: redView
Attribute: NSLayoutAttributeLeft
Multiplier: 1
Constant: 0];
Above.
Method and Macro do not provide an explanation. Please refer to the Apple Doc.