Autolayout自動(dòng)布局實(shí)現(xiàn)的方法一:storyboard的實(shí)現(xiàn)和代碼實(shí)現(xiàn)

Autolayout簡介

  • Autolayout是一種“自動(dòng)布局”技術(shù)癌淮,專門用來布局UI界面的玄呛, Autolayout最初的引入是在IOS6當(dāng)中,由于在Xcode 4中Autolayout自動(dòng)布局當(dāng)時(shí)是很不給力的,所以當(dāng)時(shí)并沒有得到大力推廣

  • 自iOS7(Xcode 5)開始诉探,Autolayout的開發(fā)效率得到很大的提升,蘋果官方也推薦開發(fā)者盡量使用Autolayout來布局UI界面棍厌,Autolayout當(dāng)然也能很輕松地解決屏幕適配的問題

  • Autolayout有2個(gè)核心概念:參照和約束

Autolayout自動(dòng)布局方法之一:Main.storyboard的實(shí)現(xiàn)

顧名思義肾胯,在Main.storyboard拖入可視化的控件進(jìn)行Autolayout自動(dòng)布局約束,其實(shí)在storyboard拖入控件的Autolayout

  • Autolayout常用約束面板之一
QQ20160407-2.jpg
  • Autolayout常用約束面板之二
QQ20160407-3.jpg
  • Autolayout常用約束面板之三
QQ20160407-4.jpg

Autolayout自動(dòng)布局方法之二:代碼的實(shí)現(xiàn)

  • 在實(shí)際的IOS開發(fā)項(xiàng)目中定铜,storyboard中添加的控件不是萬能的阳液,有一些功能這些拖拽的控件是無法實(shí)現(xiàn)的,那就避免不了需要用代碼來添加控件揣炕,那么代碼添加的控件又如何做Autolayout自動(dòng)布局呢帘皿?實(shí)際上就是將storyboard的可視化控件轉(zhuǎn)為代碼形式。
UIView *redView = [[UIView alloc]init];
  
  redView.backgroundColor = [UIColor redColor];
  
  redView.translatesAutoresizingMaskIntoConstraints = NO;
  
  [self.view addSubview:redView];
  
  //添加約束畸陡,距離頂部20
  NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:redView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
  [self.view addConstraint:topConstraint];
  
  //添加約束鹰溜,距離父控件左邊10
  NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:redView.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10];
  [self.view addConstraint:leftConstraint];
  
  //添加約束,距離父控件右邊10
  NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:redView.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10];
  [redView.superview addConstraint:rightConstraint];
  
  //添加約束丁恭,固定高度50
  NSLayoutConstraint *widthContraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
  [redView addConstraint:widthContraint];
  /** **********************greenView************************ */
UIView *greenView = [[UIView alloc]init]; 
greenView.backgroundColor = [UIColor greenColor];
greenView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:greenView];
  //添加約束曹动,距離頂部20
  NSLayoutConstraint *greenTopConstraint = [NSLayoutConstraint constraintWithItem:greenView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
  [self.view addConstraint:greenTopConstraint];
  //添加約束,保持在中間
  NSLayoutConstraint *greenLeftConstraint = [NSLayoutConstraint constraintWithItem:greenView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
  [self.view addConstraint:greenLeftConstraint];
  //添加約束牲览,距離父控件右邊10
  NSLayoutConstraint *greenRightConstraint = [NSLayoutConstraint constraintWithItem:greenView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:greenView.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10];
  [redView.superview addConstraint:greenRightConstraint];
  //添加約束,固定高度50
  NSLayoutConstraint *greenWidthContraint = [NSLayoutConstraint constraintWithItem:greenView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
  [greenView addConstraint:greenWidthContraint];

這一段又臭又長的代碼最終結(jié)果是怎樣呢贡必?

QQ20160407-5.jpg
QQ20160407-7.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市仔拟,隨后出現(xiàn)的幾起案子衫樊,更是在濱河造成了極大的恐慌,老刑警劉巖利花,帶你破解...
    沈念sama閱讀 222,807評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炒事,死亡現(xiàn)場離奇詭異臀栈,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)羡洛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門挂脑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人崭闲,你說我怎么就攤上這事威蕉〉蠹螅” “怎么了韧涨?”我有些...
    開封第一講書人閱讀 169,589評(píng)論 0 363
  • 文/不壞的土叔 我叫張陵虑粥,是天一觀的道長如孝。 經(jīng)常有香客問我娩贷,道長,這世上最難降的妖魔是什么彬祖? 我笑而不...
    開封第一講書人閱讀 60,188評(píng)論 1 300
  • 正文 為了忘掉前任储笑,我火速辦了婚禮甜熔,結(jié)果婚禮上突倍,老公的妹妹穿的比我還像新娘盆昙。我一直安慰自己烧颖,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著跳夭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪币叹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,785評(píng)論 1 314
  • 那天踩衩,我揣著相機(jī)與錄音贩汉,去河邊找鬼。 笑死匹舞,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的赐稽。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼晰绎,長吁一口氣:“原來是場噩夢啊……” “哼括丁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起躏将,我...
    開封第一講書人閱讀 40,167評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤祸憋,失蹤者是張志新(化名)和其女友劉穎会宪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體掸鹅,經(jīng)...
    沈念sama閱讀 46,698評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評(píng)論 3 343
  • 正文 我和宋清朗相戀三年葵姥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了句携。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,912評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡削咆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出拨齐,到底是詐尸還是另有隱情昨寞,我是刑警寧澤,帶...
    沈念sama閱讀 36,572評(píng)論 5 351
  • 正文 年R本政府宣布援岩,位于F島的核電站,受9級(jí)特大地震影響蹂匹,放射性物質(zhì)發(fā)生泄漏凹蜈。R本人自食惡果不足惜限寞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評(píng)論 3 336
  • 文/蒙蒙 一履植、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧玫霎,春花似錦妈橄、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叉钥。三九已至,卻和暖如春投队,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背敷鸦。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留钞螟,地道東北人谎碍。 一個(gè)月前我還...
    沈念sama閱讀 49,359評(píng)論 3 379
  • 正文 我出身青樓洞焙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親澡匪。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容