Apple's native automatic layout scheme is too cumbersome to introduce a simple and useful third-party framework.
1//Set constraint 2-(Nsarray *) Mas_makeconstraints: (void (^) (Masconstraintmaker *)) Block;3 4//If a constraint has been previously, update the new constraint, or add a constraint if there is no constraint 5 -(Nsarray *) Mas_updateconstraints: (void (^) (Masconstraintmaker *)) BLOCK;6 7//Remove all previous constraints, add new constraint 8-(Nsarray *) Mas_ Remakeconstraints: (void (^) (Masconstraintmaker *make)) block;
Note:
The Translatesautoresizingmaskintoconstraints property of the custom control should be set to no before use;
Add two controls
UIView *blueview = [[UIView alloc] init];
Blueview.backgroundcolor = [Uicolor Bluecolor];
Blueview.translatesautoresizingmaskintoconstraints = NO;
[Self.view Addsubview:blueview];
UIView *redview = [[UIView alloc] init];
Redview.backgroundcolor = [Uicolor Redcolor];
Redview.translatesautoresizingmaskintoconstraints = NO;
[Self.view Addsubview:redview];
Set constraints for Blue view
[Blueview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.left.equalTo (self.view.mas_left). Offset (30);//And the left margin of the parent view is 30;
Make.bottom.equalTo (Self.view.mas_bottom). Offset (-30);//And the bottom spacing of the parent view is 30;
Make.right.equalTo (redview.mas_left). Offset (-30);//And a red view with a spacing of 30;
Make.height.mas_equalTo (50);//Blue view has a height of 50
}];
To set a constraint on a red view
[Redview mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.right.equalTo (self.view.mas_right). Offset (-30);//And the right-hand spacing of the parent view is 30;
Make.bottom.equalTo (Blueview.mas_bottom);//align with the bottom of the blue view
Make.height.equalTo (blueview.mas_height);//The height of the blue view is equal to
Make.width.equalTo (blueview.mas_width);//And the width of the blue view is equal
}];
Use third-party framework masonry for simple use of automatic layouts