To add a constraint to a view, its dependent constraints must first exist, cannot rely on the code after the constraints, or cause unpredictable results, the following code can achieve the desired effect
- (void) makeconstraints {__weaktypeof(self) weakself =Self ; [Self.photomorebutton mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.right.equalTo (Weakself.photobutton). With.offset (-6); Make.centerY.equalTo (Weakself.photobutton); Make.width.and.height.equalTo (@ -); }]; [Self.photoimageview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.centerY.equalTo (Weakself.photobutton); Make.right.equalTo (WeakSelf.photoMoreButton.mas_left); Make.width.and.height.equalTo (@ -); }];}
But if you add the order of the constraints upside down, the following
- (void) makeconstraints {__weaktypeof(self) weakself =Self ; [Self.photoimageview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.centerY.equalTo (Weakself.photobutton); Make.right.equalTo (WeakSelf.photoMoreButton.mas_left); Make.width.and.height.equalTo (@ -); }]; [Self.photomorebutton mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.right.equalTo (Weakself.photobutton). With.offset (-6); Make.centerY.equalTo (Weakself.photobutton); Make.width.and.height.equalTo (@ -); }];}
The resulting effect is as
When you add a constraint, Photoimageview relies on Photomorebutton, which is not set at the time, so the result of the Photomorebutton is incorrect, so be careful when using masonry!
Masonry adding constraints to the order of attention