上次我们学到关于autolayout的代码使用,但是可能很多人写着写着就不想写了,为什么呢,代码量太多了 ,在此我附上我写的两个布局,代码如下,然后我们再去研究一下PureLayout怎么用的:
NSLayoutConstraint *constraint1=nil;
NSLayoutConstraint *constraint2=nil;
NSLayoutConstraint *constraint3=nil;
NSLayoutConstraint *constraint4=nil;
//设置登录label布局
self.toplab.translatesAutoresizingMaskIntoConstraints=NO;
constraint1=[NSLayoutConstraint constraintWithItem:self.toplab attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:40];
constraint2=[NSLayoutConstraint constraintWithItem:self.toplab attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0];
NSArray *topconstarray=@[constraint1,constraint2];
[self.view addConstraints:topconstarray];
//设置帐号输入框布局
self.accountField.translatesAutoresizingMaskIntoConstraints=NO;
constraint1=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.toplab attribute:NSLayoutAttributeTop multiplier:1.0f constant:20];
constraint2=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10];
constraint3=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:10];
constraint4=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30];
NSArray *accountconstarray=@[constraint1,constraint2,constraint3,constraint4];
[self.view addConstraints:accountconstarray];
//用autolayout代码太多了,受不了了,下面的就用第三方purelayout了
现在我们来学习如何使用PureLayout,其实PureLayout前身是(UIView+AutoLayout),作者是同一人,为什么推荐使用这个第三方呢?因为它封装都比较形象,并且接近底层,对理解AutoLayout的原理和掌握内涵更加的有益。
接下来我们来看一下如何使用吧:
首先我们下载PureLayout:http://www.oschina.net/p/purelayout
在头文件导入PureLayout.h文件
然后我们就来研究一下每一个方法的意思吧
首先我们在 [super viewDidLoad]之后创建一个UIView,UILabel 和UIButton来进行说明:
UIView *myview=[[UIView alloc]init];
UILabel *label=[[UILabel alloc]init];
UIButton *btn=[[UIButton alloc]init];
myview.translatesAutoresizingMaskIntoConstraints=NO;
label.translatesAutoresizingMaskIntoConstraints=NO;
btn.translatesAutoresizingMaskIntoConstraints=NO;
[self.view addSubview:myview];
[self.view addSubview:label];
[self.view addSubview:btn];
接下来我们就来看看purelayout封装了哪些好用的布局方法:
// myview左边距离父视图左边 10 点.
[myview autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10.f];
// myview1顶边距离父视图顶部 10 点.
[myview autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10.f];
// myview的左边位于btn的右边 10 点的地方.
[myview autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:btn withOffset:10.f];
// myview的右边位于btn左边 -10 点的地方, 从右往左、从下往上布局时数值都是负的。
[myview autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:btn withOffset:-10.f];
// 根据label的固有内容尺寸设置它的尺寸
[label autoSetDimensionsToSize:CGSizeMake(10.f, 10.f)];
// myview、label、btn水平对齐。(意思就是说只需要设置其中一个的垂直约束(y)即可)(这个可以设置多个view水平对齐)
[@[myview, label, btn] autoAlignViewsToAxis:ALAxisHorizontal];
//myview,label水平对齐(这个是针对两个view的水平对齐)
[myview autoAlignAxis:ALAxisHorizontal toSameAxisOfView:label];
// myview顶部距离label的底部 10 点.
[myview autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:label withOffset:10.f];
// myview左边紧贴父视图左边
[myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
// myview的宽度等于父视图的宽度
[myview autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.view];
// myview的宽度设置为50
[myview autoSetDimension:ALDimensionWidth toSize:30];
// myview与label左对齐
[myview autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:label];
// myview与label水平对齐
[myview autoAlignAxis:ALAxisHorizontal toSameAxisOfView:label];
//myview右边雨label左边的距离大于等于20
[myview autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:label withOffset:20.f relation:NSLayoutRelationGreaterThanOrEqual];
//myview和btn同宽(同高也一样)
[@[myview,btn] autoMatchViewsDimension:ALDimensionWidth];//这种可以设置多个view同宽
[myview autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:btn];//这种只能设置两个view同宽
当然这里只是列举了一些常用的方法,其实只要懂得autolayout的基本原理,学会了autolayout,PureLayout中提供的方法都能推算出什么意思。怎么样,用PureLayout会比autolayout简化很多代码量吧!
注意:这里我主要讲一下NSLayoutAttributeLeft ,NSLayoutAttributeRight,和NSLayoutAttributeLeading,NSLayoutAttributeTrailing之间的区别。
其实在中国, NSLayoutAttributeLeft 和 NSLayoutAttributeLeading 是一个效果的,因为我们布局习惯从左到右。但在有些国家地区,NSLayoutAttributeRight和NSLayoutAttributeLeading 是一个效果,布局习惯从右往左。这点我们大可放心,建议大家还是使用NSLayoutAttributeLeading,NSLayoutAttributeTrailing来表示左和右。
我们点击它的方法进去先看一下:
- (NSLayoutConstraint *)autoAlignAxisToSuperviewMarginAxis:(ALAxis)axis
{
self.translatesAutoresizingMaskIntoConstraints = NO;
ALView *superview = self.superview;
NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
ALMarginAxis marginAxis = [NSLayoutConstraint al_marginAxisForAxis:axis];
return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)marginAxis ofView:superview];
}
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
#pragma mark Pin Edges to Superview
/**
Pins the given edge of the view to the same edge of its superview.
@param edge The edge of this view and its superview to pin.
@return The constraint added.
*/
- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge
{
return [self autoPinEdgeToSuperviewEdge:edge withInset:0.0];
}
/**
Pins the given edge of the view to the same edge of its superview with an inset.
@param edge The edge of this view and its superview to pin.
@param inset The amount to inset this view's edge from the superview's edge.
@return The constraint added.
*/
- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset
{
return [self autoPinEdgeToSuperviewEdge:edge withInset:inset relation:NSLayoutRelationEqual];
}
发现这些方法都会返回一个NSLayoutConstraint。那么更新就好办了
//首先定义好一个NSLayoutConstraint属性
@property (nonatomic, strong)NSLayoutConstraint *myyueshu;
//如果我们点击按钮需要更改某个view的高度从100到200
UIView *myview = [[UIView alloc]init];
self.myview = myview;
[self.view addSubview:myview];
myview.backgroundColor = [UIColor redColor];
[self.view addSubview:myview];
myview.translatesAutoresizingMaskIntoConstraints = NO;
[myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[myview autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
[myview autoPinEdgeToSuperviewEdge:ALEdgeTop];
// [myview autoSetDimension:ALDimensionHeight toSize:100];
self.myyueshu = [myview autoSetDimension:ALDimensionHeight toSize:100];
UIButton *btn = [[UIButton alloc]init];
[self.view addSubview:btn];
btn.frame = CGRectMake(10, 400, 40, 40);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
- (void)btnclick{
self.myyueshu.constant = 200;
}
1.以上代码就是把设置高度从原先的100变为200,我们直接改变了它的约束数值。
- (void)btnclick{
[self.myyueshu autoRemove];
self.myyueshu = [self.myview autoSetDimension:ALDimensionHeight toSize:200];
}
2.以上代码,是先把原先的那个约束移除,然后重新添加新的约束。如果我们需要把所有约束都重写,以上代码也可以做到,只是要定义4个约束属性,然后各个移除而已。当然我们也可以用别的方法:
//首先定义好一个数组NSArray
@property (nonatomic, strong) NSArray *contentConstrains;
//接着在viewDidLoad中实现以下代码:
UIView *myview = [[UIView alloc]init];
self.myview = myview;
[self.view addSubview:myview];
myview.backgroundColor = [UIColor redColor];
[self.view addSubview:myview];
myview.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *s1 = [myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
NSLayoutConstraint *s2 = [myview autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
NSLayoutConstraint *s3 = [myview autoPinEdgeToSuperviewEdge:ALEdgeTop];
NSLayoutConstraint *s4 = [myview autoSetDimension:ALDimensionHeight toSize:100];
self.contentConstrains = [NSArray arrayWithObjects:s1,s2,s3,s4, nil];
UIButton *btn = [[UIButton alloc]init];
[self.view addSubview:btn];
btn.frame = CGRectMake(10, 400, 40, 40);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
- (void)btnclick{
if(self.contentConstrains != nil){
[self.contentConstrains autoRemoveConstraints];
}
[self.myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[self.myview autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
[self.myview autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:30];
[self.myview autoSetDimension:ALDimensionHeight toSize:200];
}
这样也可以实现整体更改约束。