因為之前做過android開發,android的有種布局方式叫做relativelayout,使用起來非常靈活。2023年開始接觸ios的時候,發現這種基於座標的絕對布局方式非常不靈活,所以還是按照android的relativelayout的思想進行,控制項的座標均採用相對布局的關係,比如要實現這個布局:
使用相對布局思想如下(比較麻煩的地方是需要各種座標相對位置的計算)
c**
- (void)viewdidload
從上面的**可以看出,每個控制項的布局都是根據父控制項或兄弟控制項的座標位置和尺寸進行的,這樣以後維護會很方便。
今天看了一下ios使用masonary進行autolayout布局方式,發現和android的relativelayout非常類似,於是試了一下,效果很好:
c**
- (void)viewdidload ];
uiview *line = [[uiview alloc] init];
[line setbackgroundcolor:[uicolor whitecolor]];
[_phonenofield addsubview:line];
[line mas_makeconstraints:^(masconstraintmaker *make) ];
//驗證碼按鈕
_verifybutton = [[wmbutton alloc] init];
[_verifybutton setbackgroundimage:[uiimage imagenamed:@"verify_btn"] forstate:uicontrolstatenormal];
[_verifybutton settitlecolor:[uicolor colorwithhexstring:font_color_light_green] forstate:uicontrolstatedisabled];
[_verifybutton settitle:@"獲取驗證碼" forstate:uicontrolstatenormal];
[self.view addsubview:_verifybutton];
[_verifybutton setenabled:no];
[_verifybutton mas_makeconstraints:^(masconstraintmaker *make) ];
//驗證碼輸入框
_verifynofield = [[uitextfield alloc] init];
[_verifynofield settextcolor:[uicolor whitecolor]];
[_verifynofield setclearbuttonmode:uitextfieldviewmodewhileediting];
[_verifynofield setkeyboardtype:uikeyboardtypedecimalpad];
[_verifynofield setplaceholder:@"輸入驗證碼"];
[_verifynofield setfont:[uifont fontwithname:@"helvetica" size:font_size_24]];
[_verifynofield addtarget:self action:@selector(updatebtnstate) forcontrolevents:uicontroleventeditingchanged];
[self.view addsubview:_verifynofield];
[_verifynofield mas_makeconstraints:^(masconstraintmaker *make) ];
uiview *line1 = [[uiview alloc] init];
[line1 setbackgroundcolor:[uicolor whitecolor]];
[_verifynofield addsubview:line1];
[line1 mas_makeconstraints:^(masconstraintmaker *make) ];
//提交按鈕
_loginbtn = [[wmbutton alloc] init];
[_loginbtn setbackgroundimage:[uiimage imagenamed:@"login_btn"] forstate:uicontrolstatenormal];
[self.view addsubview:_loginbtn];
[_loginbtn addtarget:self action:@selector(dologin) forcontrolevents:uicontroleventtouchupinside];
[_loginbtn setenabled:no];
[_loginbtn mas_makeconstraints:^(masconstraintmaker *make) ];
}
**:
Auto Layout的初步運用。
1.想要做到自適應,我們就得先確定控制項相對於父類檢視位置以及寬高,也就是確定frame。2.那麼自適應無非就是加些約束,來控制ui在父類檢視中的位置以及寬高。3.目前可以直接在storyboard中直接新增約束條件。如圖 4.點中控制項,按ctrl 滑鼠左鍵,即可增加約束條件。5.約束條件分為3類...
Android對於AutoLayout的優化
最近在使用鴻洋大神寫的autolayout框架時,發現只支援linearlayout,relativelayout,framelayout這三種常用布局。但是我最近剛好準備使用drawerlayout發現無法適配。這讓我很尷尬阿 偶然翻看原始碼,發現此事另有轉機阿。下面我們來看一下原始碼 publi...
AutoLayout的使用及介紹
為什麼會出現autoresizingmask和autolayout?1.適配 適應 相容各種不同的情況 系統適配 新的方法 舊的方法廢棄 螢幕適配 針對不同大小螢幕尺寸進行適配 1 點 畫素的關係 在使用者眼中 螢幕是由無數個畫素組成的 畫素越多,螢幕越清晰 在開發者眼中 螢幕是由無數個點組成的,點...