PureLayout下載地址
https://github.com/PureLayout/PureLayout
PureLayout布局使用
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 點的地方, 從右往左、從下往上布局時數(shù)值都是負的。
[myview autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:btn withOffset:-10.f];
// 根據(jù)label的固有內(nèi)容尺寸設(shè)置它的尺寸
[label autoSetDimensionsToSize:CGSizeMake(10.f, 10.f)];
// myview、label、btn水平對齊。(意思就是說只需要設(shè)置其中一個的垂直約束(y)即可)(這個可以設(shè)置多個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的寬度設(shè)置為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];//這種可以設(shè)置多個view同寬
[myview autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:btn];//這種只能設(shè)置兩個view同寬
注意:這里我主要講一下NSLayoutAttributeLeft ,NSLayoutAttributeRight,和NSLayoutAttributeLeading,NSLayoutAttributeTrailing之間的區(qū)別。
其實在中國, NSLayoutAttributeLeft 和 NSLayoutAttributeLeading 是一個效果的,因為我們布局習慣從左到右。但在有些國家地區(qū),NSLayoutAttributeRight和NSLayoutAttributeLeading 是一個效果,布局習慣從右往左。這點我們大可放心,建議大家還是使用NSLayoutAttributeLeading,NSLayoutAttributeTrailing來表示左和右。
PureLayout更改約束
我們點擊它的方法進去先看一下:
- (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];
}
發(fā)現(xiàn)這些方法都會返回一個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];
把設(shè)置高度從原先的100變?yōu)?00,我們直接改變了它的約束數(shù)值
- (void)btnclick{
self.myyueshu.constant = 200;
}
或者先把原先的那個約束移除,然后重新添加新的約束。
- (void)btnclick{
[self.myyueshu autoRemove];
self.myyueshu = [self.myview autoSetDimension:ALDimensionHeight toSize:200];
}
如果我們需要把所有約束都重寫,以上代碼也可以做到,只是要定義4個約束屬性,然后各個移除而已。當然我們也可以用別的方法
//首先定義好一個數(shù)組NSArray
@property (nonatomic, strong) NSArray *contentConstrains;
//接著在viewDidLoad中實現(xiàn)以下代碼:
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];
這樣也可以實現(xiàn)整體更改約束。
- (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];
}