说到iOS
自动布局,有很多的解决办法。有的人使用xib/storyboard
自动布局,也有人使用frame
来适配。对于前者,笔者并不喜欢,也不支持。对于后者,更是麻烦,到处计算高度、宽度等,千万大量代码的冗余,对维护和开发的效率都很低。
笔者在这里介绍纯代码自动布局的第三方库:Masonry
。这个库使用率相当高,在全世界都有大量的开发者在使用,其star
数量也是相当高的。
效果图
本节详解Masonry
的以动画的形式更新约束的基本用法,先看看效果图:
我们这里初始按钮是一个很小的按钮,点击就不断放大,最大就放大到全屏幕。
核心代码
看下代码:
@interfaceTotalUpdateController() @property(nonatomic,strong)UIView*purpleView; @property(nonatomic,strong)UIView*orangeView; @property(nonatomic,assign)BOOLisExpaned; @end @implementationTotalUpdateController -(void)viewDidLoad{ [superviewDidLoad]; UIView*purpleView=[[UIViewalloc]init]; purpleView.backgroundColor=UIColor.purpleColor; purpleView.layer.borderColor=UIColor.blackColor.CGColor; purpleView.layer.borderWidth=2; [self.viewaddSubview:purpleView]; UITapGestureRecognizer*tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(onTap) ]; [purpleViewaddGestureRecognizer:tap]; self.purpleView=purpleView; UIView*orangeView=UIView.new; orangeView.backgroundColor=UIColor.orangeColor; orangeView.layer.borderColor=UIColor.blackColor.CGColor; orangeView.layer.borderWidth=2; [self.viewaddSubview:orangeView]; self.orangeView=orangeView; //这里,我们不使用updateViewConstraints方法,但是我们一样可以做到。 //不过苹果推荐在updateViewConstraints方法中更新或者添加约束的 [selfupdateWithExpand:NOanimated:NO]; UILabel*label=[[UILabelalloc]init]; label.numberOfLines=0; label.textColor=[UIColorredColor]; label.font=[UIFontsystemFontOfSize:16]; label.textAlignment=NSTextAlignmentCenter; label.text=@"点击purple部分放大,orange部分最大值250,最小值90"; [self.purpleViewaddSubview:label]; [labelmas_makeConstraints:^(MASConstraintMaker*make){ make.bottom.mas_equalTo(0); make.left.right.mas_equalTo(0); }]; } -(void)updateWithExpand:(BOOL)isExpandedanimated:(BOOL)animated{ self.isExpaned=isExpanded; [self.purpleViewmas_updateConstraints:^(MASConstraintMaker*make){ make.left.top.mas_equalTo(20); make.right.mas_equalTo(-20); if(isExpanded){ make.bottom.mas_equalTo(-20); }else{ make.bottom.mas_equalTo(-300); } }]; [self.orangeViewmas_updateConstraints:^(MASConstraintMaker*make){ make.center.mas_equalTo(self.purpleView); //这里使用优先级处理 //设置其最大值为250,最小值为90 if(!isExpanded){ make.width.height.mas_equalTo(100*0.5).priorityLow(); }else{ make.width.height.mas_equalTo(100*3).priorityLow(); } //最大值为250 make.width.height.lessThanOrEqualTo(@250); //最小值为90 make.width.height.greaterThanOrEqualTo(@90); }]; if(animated){ [self.viewsetNeedsUpdateConstraints]; [self.viewupdateConstraintsIfNeeded]; [UIViewanimateWithDuration:0.5animations:^{ [self.viewlayoutIfNeeded]; }]; } } -(void)onTap{ [selfupdateWithExpand:!self.isExpanedanimated:YES]; } @end
讲解
移除之前的所有约束,然后添加新约束的方法是:mas_remakeConstraints
。
这里展开与收起的关键代码在这里.设置最大最小值,这样就不会超出我们预期的范围。
//最大值为250 make.width.height.lessThanOrEqualTo(@250); //最小值为90 make.width.height.greaterThanOrEqualTo(@90);
我们设置其固定的宽高,并且设置其优先级为最低,以保证我们所设置的最大最小值始终生效。
想要更新约束时添加动画,就需要调用关键的一行代码:setNeedsUpdateConstraints
,这是选择对应的视图中的约束需要更新。
对于updateConstraintsIfNeeded
这个方法并不是必须的,但是有时候不调用就无法起到我们的效果。但是,官方都是这么写的,从约束的更新原理上讲,这应该写上。我们要使约束立即生效,就必须调用layoutIfNeeded
此方法。看下面的方法,就是动画更新约束的核心代码:
//告诉self.view约束需要更新 [self.viewsetNeedsUpdateConstraints]; //调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用 [self.viewupdateConstraintsIfNeeded]; [UIViewanimateWithDuration:0.3animations:^{ [self.viewlayoutIfNeeded]; }];
源代码
大家可以到笔者的github
下载源代码:MasonryDemo
温馨提示:本节所讲内容对应于RemakeConstraintsController
中的内容
推荐阅读
CocoaChina上有一篇文章讲得也不错,叫Masonry介绍与使用实践:快速上手Autolayout和IOS自适应前段库-Masonry的使用。
文章中所讲的内容属于很基础的内容,如果有任何疑问可以联系博主哦!