概述
以前一直聽(tīng)說(shuō)autoLayout
宏多,跟xib
、storybord
無(wú)縫結(jié)合使用
姻乓,設(shè)置各種約束條件來(lái)達(dá)到適配的目的砂碉。比如設(shè)置一個(gè)view
的約束條件為距離superview
上下左右邊距都是10
點(diǎn),這樣就可以確定這個(gè)view
在superview
中的位置特碳,而且不管在哪個(gè)設(shè)備都是距離superview
上下左右邊距10
點(diǎn)诚亚。
感覺(jué)是特別好使,不過(guò)我一直是純代碼編寫(xiě)項(xiàng)目午乓,完全不知道怎么使用autoLayout
站宗,最近決定研究研究怎么適配。
一開(kāi)始學(xué)習(xí)autoLayout
毫無(wú)頭緒益愈,各種查資料看官方文檔研究官方demo梢灭,從一開(kāi)始不知道NSLayoutConstraint
這個(gè)類,不知道VFL(Visual Format Language )
蒸其,現(xiàn)在可以做簡(jiǎn)單的適配工作敏释,今天就分享下學(xué)習(xí)經(jīng)驗(yàn)。
autoLayout
這里引用喵神的WWDC 2012 Session筆記——202, 228, 232 AutoLayout(自動(dòng)布局)入門的一段文字
使用一句Apple的官方定義的話
AutoLayout
是一種基于約束的摸袁,描述性的布局系統(tǒng)钥顽。 Auto Layout Is a Constraint-Based, Descriptive Layout System.
關(guān)鍵詞:
- 基于約束 - 和以往定義
frame
的位置和尺寸不同,AutoLayout
的位置確定是以所謂相對(duì)位置的約束來(lái)定義的靠汁,比如x
坐標(biāo)為superView
的中心蜂大,y
坐標(biāo)為屏幕底部上方10
像素等 - 描述性 - 約束的定義和各個(gè)
view
的關(guān)系使用接近自然語(yǔ)言或者可視化語(yǔ)言(稍后會(huì)提到)的方法來(lái)進(jìn)行描述 - 布局系統(tǒng) - 即字面意思闽铐,用來(lái)負(fù)責(zé)界面的各個(gè)元素的位置。
在IB
中拖拽的方法就不說(shuō)了奶浦,網(wǎng)上的教程很詳細(xì)兄墅。
這里說(shuō)一下純代碼實(shí)現(xiàn)autoLayout
的方法
要實(shí)現(xiàn)自動(dòng)布局,必須關(guān)掉view
的AutoresizeingMask
view.translatesAutoresizingMaskIntoConstraints = NO;
用到NSLayoutConstraint
的第一個(gè)類方法
+(instancetype)constraintWithItem:(id)view1 //約束左邊的視圖
attribute:(NSLayoutAttribute)attr1 //view1的屬性
relatedBy:(NSLayoutRelation)relation //左右視圖的關(guān)系
toItem:(id)view2 //約束右邊的視圖
attribute:(NSLayoutAttribute)attr2 //view2的屬性
multiplier:(CGFloat)multiplier //乘數(shù)
constant:(CGFloat)c; //常量
公式是這樣的:view1.attr1 = view2.attr2 * multiplier + constant
其中NSLayoutAttribute
屬性
typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = 1, //左邊
NSLayoutAttributeRight, //右邊
NSLayoutAttributeTop, //頂部
NSLayoutAttributeBottom, //底部
NSLayoutAttributeLeading, //首部
NSLayoutAttributeTrailing, //尾部
NSLayoutAttributeWidth, //寬度
NSLayoutAttributeHeight, //高度
NSLayoutAttributeCenterX, //X軸中心
NSLayoutAttributeCenterY, //Y軸中心
NSLayoutAttributeBaseline, //基線
NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,
NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),
//iOS8的這里不說(shuō)明了财喳,我也不知道察迟。
NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeNotAnAttribute = 0 //無(wú)屬性
};
NSLayoutRelation
關(guān)系
typedef NS_ENUM(NSInteger, NSLayoutRelation) {
NSLayoutRelationLessThanOrEqual = -1, //小于等于
NSLayoutRelationEqual = 0, //等于
NSLayoutRelationGreaterThanOrEqual = 1, //大于等于
};
代碼
創(chuàng)建view
UIView *view = [[UIView alloc] init]; //這里不需要設(shè)置frame
view.backgroundColor = [UIColor brownColor];
view.translatesAutoresizingMaskIntoConstraints = NO; //要實(shí)現(xiàn)自動(dòng)布局,必須把該屬性設(shè)置為NO
[self.view addSubview:view];
添加約束
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1
constant:20]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1
constant:-10]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:30]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-20]];
效果圖
這個(gè)方法我用的少耳高,畢竟太長(zhǎng)扎瓶,主要還是用的第二個(gè)方法,下篇介紹泌枪。
分享兩個(gè)第三方
UIView-AutoLayout
Masonry
Masonry介紹與使用實(shí)踐(快速上手Autolayout)