iOS頁(yè)面布局及適配
一、固定布局frame(x, y, w, h)
UIEdgeInsets edge = UIEdgeInsetsMake(10, 10, 10, 10);
CGSize superSize = view.superview.frame.size;
CGFloat width = superSize.width - edge.left - edge.right;
CGFloat heitht = superSize.height - edge.top - edge.bottom;
view.frame = CGRectMake(edge.left, edge.top, width, heitht);
局限性:位置固定,不能隨父view大小的改變而改變
二、Autoresizing Mask
enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin 自動(dòng)調(diào)整與superView左邊的距離,保證與左邊的距離和右邊的距離和原來(lái)距左邊和右邊的距離的比例不變。比如原來(lái)距離為20,30,調(diào)整后的距離應(yīng)為60,90,即60/20=90/30。
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight比較常用。
適用性:讓橫豎屏適配相對(duì)簡(jiǎn)單
讓子控件可以隨父控件的行為發(fā)生相應(yīng)的改變
局限性:只能解決子控件和父控件的相對(duì)關(guān)系問(wèn)題, 不能解決兄弟控件的相對(duì)關(guān)系問(wèn)題
三、AutoLayout
iOS6開(kāi)始引入AutoLayout,是一種基于約束的、描述性的布局系統(tǒng),主要功能是使用約束,對(duì)視圖進(jìn)行相對(duì)布局,以適應(yīng)不同屏幕尺寸的變換及屏幕的旋轉(zhuǎn)
iOS7時(shí),因?yàn)橹挥?.5英寸及4英寸兩種尺寸,寬度一樣,高度不同,比較好適配,所以AutoLayout并沒(méi)有普及。
iOS8時(shí),添加了4.7英寸,三種屏幕尺寸,必須考慮適配問(wèn)題。
[self addConstraints:@[ [NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.0]];
view1.attr1 = view2.attr2 * multiplier + constant
對(duì)于兩個(gè)同層級(jí)view之間的約束關(guān)系,添加到他們的父view上
對(duì)于兩個(gè)不同層級(jí)view之間的約束關(guān)系,添加到他們最近的共同父view上
對(duì)于有層次關(guān)系的兩個(gè)view之間的約束關(guān)系,添加到層次較高的父view上
容易出現(xiàn)的問(wèn)題:
添加約束之前,一定要保證相關(guān)控件都已經(jīng)在各自的父控件上, 否則crash
約束不夠(添加必要的約束條件或者調(diào)整優(yōu)先級(jí))
約束出現(xiàn)沖突(刪掉沖突的約束條件)
四、Masonry
Autolayout的方法太繁瑣,Masonry框架可以讓我們更輕松的為控件添加約束。
define MAS_SHORTHAND_GLOBALS使用全局宏定義,可以使equalTo等效于mas_equalTo
define MAS_SHORTHAND使用全局宏定義, 可以在調(diào)用masonry方法的時(shí)候不使用mas_前綴
1、Masonry使用方法
// 僅僅會(huì)添加新的約束
[myView mas_makeConstraints:^(MAConstraintMaker *make){
// 在這個(gè)block里面, 利用make對(duì)象為控件添加約束
make.size.mas_equalTo(CGSize(100, 100)); // 尺寸約束
make.center.mas_equalTo(self.view); // 位置約束: 居中
}];
// 將以前所有的約束都刪掉, 重新添加約束
[myView mas_remakeConstraints:^(MAConstraintMaker *make){
}];
// 覆蓋以前的某些特定的約束
[myView mas_updateConstraints:^(MAConstraintMaker *make){
}];
2、約束的類(lèi)型
1.尺寸: width\height\size
2.邊界: left\leading\right\trailing\top\bottom\edges
3.中心點(diǎn): center\centerX\centerY\baseline
4.偏移量: offset\insets\sizeOffset\contentOffset
- priority()約束優(yōu)先級(jí)(0~1000),multipler乘因數(shù), dividedBy除因數(shù)
3、約束的關(guān)系
equalTo
lessThanOrEqualTo
greaterThanOrEqualTo
mas_equalTo:這個(gè)方法會(huì)對(duì)參數(shù)進(jìn)行封裝( 可以跟數(shù)據(jù)和對(duì)象 )
equalTo:這個(gè)方法不會(huì)對(duì)參數(shù)進(jìn)行封裝( 后面括號(hào)里面必須是對(duì)象 )
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).with.offset(padding.top);
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];