iOS如果項目中不用xib或者storyboard的話刹孔,給view做約束一般都是用第三方庫
Masonry
赊级,為什么不用系統(tǒng)提供的AutoLayout呢?因為代碼太多不好用
- 今天給大家介紹一個蘋果iOS9后更新的一個布局的好用的類
NSLayoutAnchor
分別用 NSLayoutConstraint
NSLayoutAnchor
和 Masonry
來進行如下圖所示的布局
在進行布局的時候圈纺,我會分別講解下系統(tǒng)
NSLayoutConstraint
NSLayoutAnchor
兩種布局的方法使用和參數(shù)說明
至于Masonry
的使用 我就不做多介紹了(官方介紹的很清楚链沼,網(wǎng)上資料也不少)
第一種 NSLayoutConstraint
# NSLayoutConstraint 的核心布局方法
[NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
上面代碼的白話文就是
紅色view 的 centerX 等于 self.view 的 centerX 1.0倍 加 0
如果還不是很明白的話 看下圖就一目了然了
接下來我們來講一下初始化方法中各個參數(shù)的意義:
item: 要布局的view
attribute: 是一個NSLayoutAttribute枚舉默赂,可以看到他的枚舉值有 left、right括勺、bottom放可、top 等
relatedBy: 是一個NSLayoutRelation枚舉,他的枚舉值有 lessThanOrEqual(小于等于)朝刊、equal(等于)、greaterThanOrEqual(大于等于)蜈缤, 指定 view1和接下來那個參數(shù) view2兩個視圖之
間的約束關系的
toItem: 第一個view的參照view 你要參照哪個view 這個就是哪個view(本例中的是self.view控制器view)
attribute: 和第二個參數(shù)一樣拾氓,是來表示第一個視圖對第二個視圖的
參考位置 ,上下左右 還是 center等
multiplier: 是來計算兩個視圖之間約束的倍數(shù)關系
constant: 是來計算兩個視圖之間約束的倍數(shù)關系的基礎上再加一些常量
下面看約束代碼
// 布局redView
NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTopMargin multiplier:1.0 constant:10];
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200];
NSLayoutConstraint *heith = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200];
//可以單個添加約束
[self.view addConstraint:centerX];
[self.view addConstraint:top];
//也可以添加約束多個約束
[self.redView addConstraints:@[width, heith]];
// 布局blueView
NSLayoutConstraint *centerX1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
NSLayoutConstraint *top1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
NSLayoutConstraint *width1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeWidth multiplier:1.5 constant:0];
NSLayoutConstraint *heith1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeWidth multiplier:1.5 constant:0];
//iOS8 以后 NSLayoutConstraint 的類方法 也可以把約束添加到視圖上底哥,而且省掉了判斷添加到那個視圖上的問題咙鞍,避免了上面例子中因為視圖添加錯誤而導致的崩潰
[NSLayoutConstraint activateConstraints:@[centerX1,top1, width1, heith1]];
NSLayoutConstraint
添加約束注意事項
1.如果兩個視圖(也就是參數(shù) item 和 toItem)是父子關系,設置子控件的約束趾徽,約束添加到父控件上
2.如果兩個視圖(也就是參數(shù) item 和 toItem)是兄弟關系续滋,設置兩兄弟的約束,約束會添加到第一個共同的父控件上
3.如果兩個視圖(也就是參數(shù) item 和 toItem)是同一個視圖孵奶,約束會添加到自己上(一般給自己添加約束toItem為nil attribute為NSLayoutAttributeNotAnAttribute)
4.要給添加約束的view要設置translatesAutoresizingMaskIntoConstraints
為NO 否則約束不生效
UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];
// 要禁止 autoresize 意思就是遵循autoLayout拋棄原有設置的高度寬度等
// 使用autolayout的視圖必須要設置該屬性
redView.translatesAutoresizingMaskIntoConstraints = NO;
第二種 NSLayoutAnchor
ios9以后
- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor;
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor;
- (NSLayoutConstraint *)constraintLessThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor;
/* These methods return an inactive constraint of the form thisAnchor = otherAnchor + constant.
*/
- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor constant:(CGFloat)c;
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor constant:(CGFloat)c;
- (NSLayoutConstraint *)constraintLessThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor constant:(CGFloat)c;
接下來我們來講一下使用方法
比如要給redView設置一個約束疲酌,如下
[self.redView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor];
上面代碼的意思為 紅色的view的centerX和self.view的centerX相等
下面看使用NSLayoutAnchor
寫的約束代碼
[self.redView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[self.redView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20].active = YES;
[self.redView.widthAnchor constraintEqualToConstant:200].active = YES;
[self.redView.heightAnchor constraintEqualToConstant:200].active = YES;
[self.blueView.centerXAnchor constraintEqualToAnchor:self.redView.centerXAnchor].active = YES;
[self.blueView.topAnchor constraintEqualToAnchor:self.redView.bottomAnchor constant:20].active = YES;
[self.blueView.widthAnchor constraintEqualToAnchor:self.redView.widthAnchor multiplier:1.5].active = YES;
[self.blueView.heightAnchor constraintEqualToAnchor:self.redView.heightAnchor multiplier:1.5].active = YES;
NSLayoutAnchor
添加約束注意事項
1.要給添加約束的view要設置translatesAutoresizingMaskIntoConstraints
為NO 否則約束不生效 自定義view的時候也要給子view設置此屬性,總之你要給哪個view設置Layout就要給哪個view設置此屬性為NO
2.active
要設置為YES 這個是控制約束是否真正添加的開關 設置為NO的時候 約束失效
第三種 Masonry
直接上代碼了
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_topMargin).with.offset(20);
make.centerX.equalTo(self.view.mas_centerX);
make.width.height.equalTo(@200);
}];
[self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.redView.mas_bottom).offset(20);
make.centerX.equalTo(self.redView.mas_centerX);
make.width.height.equalTo(self.redView).multipliedBy(1.5);
}];