由于之前開發(fā)都是用的
xib
,并沒有過多的手寫autolayout
八酒,簡易的VFL語法也就搞定了空民,也就沒有刻意封裝,最近一個(gè)項(xiàng)目是純代碼開發(fā)丘跌,用的是第三方框架massory袭景,鏈?zhǔn)秸Z法,簡潔優(yōu)雅闭树。雖然massory是目前比較好用功能全面的布局框架耸棒,但也有一點(diǎn)遺憾,不能支持多級映射报辱。
例如:
需求:把label2放在label1的正下方与殃,距離20point。
Snip20171209_10.png
label2的高等于label1的底部偏移20point,同時(shí)寬幅疼、高米奸、centerX等于label1
massory的實(shí)現(xiàn)方式
[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(label1.mas_bottom).offset(20);
make.width.centerX.height.equalTo(label1);
}];
理想狀態(tài)的實(shí)現(xiàn)
[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.centerX.width.height.equalTo(label1.mas_bottom).offset(20);
}];
帶著這個(gè)疑問,昨天花了一天時(shí)間開始了重復(fù)造輪子爽篷,最終實(shí)現(xiàn)效果如下:
LightAutoLayout的實(shí)現(xiàn)方式一個(gè)簡單的分類
label2.top_.centX.widt_.heit_.equalTo(label1.bott_).offset(20).on_();
講解:
筆者使用了三個(gè)容器進(jìn)行數(shù)據(jù)存儲(chǔ)
@property (nonatomic, strong) NSMutableArray <NSNumber *>*layoutArrayM;//存儲(chǔ)對應(yīng)關(guān)系
@property (nonatomic, strong) NSMutableArray <NSNumber *>*equalToArrayM;//存儲(chǔ)常量
@property (nonatomic, strong) NSMutableArray <NSNumber *>*offsetArrayM;//存儲(chǔ)偏移值
取值順序悴晰,先從對應(yīng)關(guān)系數(shù)組取值,檢查是否有依賴視圖逐工,取值偏移數(shù)組铡溪,取值常量數(shù)組,思維導(dǎo)圖如下:
Snip20171209_12.png
對應(yīng)autolayout的對應(yīng)關(guān)系有三種
- 1泪喊、相對于控件本身棕硫,約束添加到自身
- 2、相對于父控件袒啼,約束添加到父控件
- 3哈扮、相對于其他視圖,約束添加到父控件
具體操作還需要在實(shí)踐中具體體會(huì)蚓再。
常用方法:
- 1滑肉、快速添加
label1.topLeft_(CGRectMake(100, 200, 70, 30));//對應(yīng)x,y,w,h
label1.around_();//等于父視圖
- 2、常量添加
label1.left_.top_.size_.constList(@(100),@(200),@(50),@(50),nil).on_();//對應(yīng)x,y,w,h
- 3对途、多級映射方法
label4的左邊等于label3的右邊同時(shí)偏移10point赦邻,寬和高等于100髓棋,centerY等于label3
label4.left_.widt_.heit_.centY.equalTo(label3.righ_).offset(10).constList(@(100),@(100),nil).on_();
下載地址:
LightAutoLayout