Objective
When iOS
it comes to automatic layout, there are a lot of workarounds. Some people use xib/storyboard
automatic layout, and others use it frame
to fit. For the former, I do not like, nor support. For the latter, it is troublesome, everywhere calculate height, width, etc., tens of millions of redundant code, the maintenance and development of the efficiency are very low.
The author introduces the third-party library of pure Code automatic layout here: Masonry
. The use of this library is very high, there are a large number of developers in the world in use, the star
number is also quite high.
This section details Masonry
the basic usage of updating constraints in animations, first look at:
Here we have the initial button is a very small button, click on the zoom, the largest zoom to the full screen.
Core code
Look at the code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
@interface remakecontraintscontroller () @property (nonatomic, strong) UIButton *growingbutton; @property (nonatomic, assign) BOOL isexpanded; @end @implementation remakecontraintscontroller - (void)viewdidload { [super viewdidload]; Self . Growingbutton = [UIButton buttonwithtype: Uibuttontypesystem]; [self. Growingbutton settitle:@ "dot me unfold" forstate: UIControlStateNormal]; self. Growingbutton. Layer. Bordercolor = Uicolor. Greencolor. Cgcolor Self . Growingbutton. Layer. BorderWidth = 3; Self . Growingbutton. BackgroundColor = [uicolor redcolor]; [self growingbutton Addtarget:self Action: @selector (ongrowbuttontaped:) Forcontrolevents:uicontroleventtouchupinside [self. View addsubview: Self. Growingbutton]; Self . isexpanded = NO; } - (void)updateviewconstraints { //Use update here is the same. //remake will remove all previous and then re-add [self. Growingbutton mas_remakeconstraints:^(masconstraintmaker *make) { Make . Top. Mas_equalto(0); Make . Left. Right. Mas_equalto(0); if (self. isexpanded) { Make . Bottom. Mas_equalto(0); } else { Make . Bottom. Mas_equalto(-); } }]; [super updateviewconstraints]; }- (void)ongrowbuttontaped:(UIButton *)sender { Self . isexpanded = ! Self. isexpanded; if (! Self. isexpanded) { [self. Growingbutton settitle:@ "dot me unfold" forstate: UIControlStateNormal]; } else { [self. Growingbutton settitle:@ "point I collect" forstate: UIControlStateNormal]; } //Tell self.view constraints need to be updated [self. View setneedsupdateconstraints]; //Call this method to tell Self.view whether the constraint needs to be updated, and if necessary, to add an animation effect [self. View updateconstraintsifneeded]; [UIView animatewithduration:0.3 animations:^{ [self. View layoutifneeded]; }];}@end |
Explain
Remove all previous constraints, and then add a new constraint by: mas_remakeConstraints
.
Here are the key codes to unfold and close up here:
1234567 |
if (self. isexpanded) { Make . Bottom. Mas_equalto(0); } else { Make . Bottom. Mas_equalto(-); } |
To add animations when you want to update a constraint, you need to invoke the key line of code: setNeedsUpdateConstraints
This is the constraint that you select in the corresponding view that needs to be updated.
updateConstraintsIfNeeded
It is not necessary for this method, but sometimes it is not possible to play our effect without invoking it. But that's what the authorities say, and it should be written on the principle of the renewal of the constraint. This method must be called when we want the constraint to take effect immediately layoutIfNeeded
. Look at the following method, which is the core code of the animation update constraint:
12345678910 |
//Tell self.view constraints need to be updated[self. View setneedsupdateconstraints]; //Call this method to tell Self.view whether the constraint needs to be updated, and if necessary, to add an animation effect[self. View updateconstraintsifneeded]; [UIView animatewithduration:0.3 animations:^{ [self. View layoutifneeded]; }]; |
Masonry remake update constraint