iOS 自動布局Masonry工具包的簡化操作

2021-08-09 11:32:49 字數 1831 閱讀 1511

特別說明,這是適合我自己的做法,其他人使用有可能引起不適,請在用過masonry的人員陪同下**。

用了masonry,感覺寫了一堆小圓點但是只是傳遞一兩個引數用起來特別麻煩,所以就偷懶吧。

這些巨集定義只能寫在 mas_makeconstraints呼叫的block裡面使用的,並且block引數變數名必須是make。

下面幾個align要這麼理解:自己向誰對齊,自己的**向誰的**對齊。

比如:alignleft( toview, margin ) 意思是,自己向toview左對齊,並且間距為margin

又比如:topalignbottom(toview,margin) 意思是,自己的頂部向toview的底部對齊,並且間距為margin。

#define makeheight( h )                    make.height.mas_equalto((h))

#define makewidth( w )                     make.width.mas_equalto((w))

#define makesize( width,height)            make.size.mas_equalto(cgsizemake((width), (height)))

#define alignleft( toview, margin)         make.left.equalto( (toview).mas_left ).with.offset((margin))

#define alignright( toview, margin)        make.right.equalto( (toview).mas_right ).with.offset((margin))

#define aligntop( toview, margin)          make.top.equalto( (toview).mas_top ).with.offset((margin))

#define alignbottom( toview, margin)       make.bottom.equalto( (toview).mas_bottom ).with.offset((margin))

#define bottomaligntop( toview, margin)    make.bottom.equalto( (toview).mas_top ).with.offset((margin))

#define topalignbottom( toview, margin)    make.top.equalto( (toview).mas_bottom ).with.offset((margin))

#define leftalignright( toview, margin)    make.left.equalto( (toview).mas_right ).with.offset((margin))

#define rightalignleft( toview, margin)    make.right.equalto( (toview).mas_left ).with.offset((margin))

#define horizontalcenter( toview )         make.centerx.equalto((toview))

#define verticalcenter( toview )           make.centery.equalto((toview))

舉例:[labelaccount mas_makeconstraints:^(masconstraintmaker *make) ];

末了,建議將這些巨集直接改寫成masconstraintmaker的方法(成員函式)比較好,那樣用起來比較安全,我這麼做比較危險。

自動布局Masonry

通過cocoapod新增masonry三方,或者手動新增三方 建立乙個view uiview view uiview new view.backgroundcolor uicolor purplecolor self view addsubview view 300 300 居中顯示的矩形 view...

Masonry布局控制項,自動換行

masonry偏移量offset答疑 make.top.equalto self.view offset 10 make.left.equalto self.view offset 10 make.bottom.equalto self.view offset 10 make.right.equal...

iOS自適應布局之Masonry 一

正因為上面說的那樣,螢幕的尺寸逐步增多,還沿用以前的方法布局顯然是不行的,這時候為布局引進新的結局辦法nsautolayout,但蘋果提供的比較複雜麻煩,所以就有人在此基礎上進行封裝,而提供一種高效的框架 masonary 如何安裝框架?還是建議用cocoapods,這裡有我之前寫的cocoapod...