? ? iOS的自動(dòng)布局,可以使用xib/storyboard和frame(即代碼)來適配遣鼓。對(duì)于xib/storyboard來說iPhone現(xiàn)在屏幕有4種尺寸,如果要針對(duì)不同屏幕進(jìn)行調(diào)整,用xib的話會(huì)比較麻煩,而且后期的維護(hù)工作將會(huì)非常的麻煩,xib適合一些布局簡(jiǎn)單而且經(jīng)常不變的界面。如果用純代碼,可以在代碼中統(tǒng)一調(diào)整,但是用代碼的話也是麻煩优质,到處計(jì)算高度、寬度等,千萬大量代碼的冗余巩螃,對(duì)維護(hù)和開發(fā)的效率都很低演怎。
? ? 所以在這里介紹純一個(gè)代碼自動(dòng)布局的第三方庫:Masonry。這個(gè)庫使用率相當(dāng)高避乏,在全世界都有大量的開發(fā)者在使用爷耀,其star數(shù)量也是相當(dāng)高的。
下載地址:github.com/CoderJackyHuang/MasonryDemo
1.1Masonry支持的一些屬性
@property?(nonatomic,?strong,?readonly)?MASConstraint?*left;左側(cè)
@property?(nonatomic,?strong,?readonly)?MASConstraint?*top上側(cè)
@property?(nonatomic,?strong,?readonly)?MASConstraint?*right;右側(cè)
@property?(nonatomic,?strong,?readonly)?MASConstraint?*bottom;下側(cè)
@property?(nonatomic,?strong,?readonly)?MASConstraint?*leading;首部
@property?(nonatomic,?strong,?readonly)?MASConstraint?*trailing;尾部
@property?(nonatomic,?strong,?readonly)?MASConstraint?*width;寬
@property?(nonatomic,?strong,?readonly)?MASConstraint?*height;高
@property?(nonatomic,?strong,?readonly)?MASConstraint?*centerX;橫向居中
@property?(nonatomic,?strong,?readonly)?MASConstraint?*centerY;縱向居中
@property?(nonatomic,?strong,?readonly)?MASConstraint?*baseline;文本基線
這些屬性與NSLayoutAttrubute的屬性是一樣的拍皮,比如NSLayoutAttrubuteLeft歹叮,NSLayoutAttrubuteTop,NSLayoutAttrubuteRight..........如下:
2.Masonry的一些簡(jiǎn)單的實(shí)例
2.1居中顯示一個(gè)view
2.2一個(gè)對(duì)象相對(duì)于superView的約束
2.3讓兩個(gè)高度為150的view垂直居中且等寬且等間隔排列間隔為10(自動(dòng)計(jì)算其寬度,高度)
2.4在UIScrollView順序排列一些view并自動(dòng)計(jì)算contentSize
? ? 從scrollView的scrollIndicator可以看出scrollView的內(nèi)部已如我們所想排列好了铆帽,這里的關(guān)鍵就在于container這個(gè)view起到了一個(gè)中間層的作用能夠自動(dòng)的計(jì)算uiscrollView的contentSize
2.5比例(multipliedBy)
總結(jié):
通過上面的例子咆耿,已經(jīng)把Masonry的功能介紹的差不多了,如果還有興趣的話可以自行研究官方的demo爹橱。Masonry是一個(gè)非常優(yōu)秀的autolayout庫 能夠節(jié)省大量的開發(fā)和學(xué)習(xí)時(shí)間 尤其適合純代碼的iOSer 在iPhone6發(fā)布后引發(fā)的適配潮中 Masonry一定可以助你一臂之力 萨螺。