NSLayoutConstraint

1.Demo

/*What's wrong with NSLayoutConstraints?

Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. However creating constraints from code is verbose and not very descriptive. Imagine a simple example in which you want to have a view fill its superview but inset by 10 pixels on every side*/

//
UIView *superview = self.view;

UIView *baseView  = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
baseView.backgroundColor = [UIColor yellowColor];
[superview addSubview:baseView];

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];

UIEdgeInsets padding = UIEdgeInsetsMake(100, 100, 100, 100);

[superview addConstraints:@[
                       //view1 constraints
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeTop
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeTop
                                                        multiplier:1.0
                                                          constant:padding.top],
                            
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeLeft
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeLeft
                                                        multiplier:1.0
                                                          constant:padding.left],
                            
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeBottom
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeBottom
                                                        multiplier:1.0
                                                          constant:-padding.bottom],
                            
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeRight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeRight
                                                        multiplier:1
                                                          constant:-padding.right],
                            
                            ]];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 以前一直聽說autoLayout,跟xib、storybord無縫結(jié)合使用,設(shè)置各種約束條件來達(dá)到適配的目的...
    guaker閱讀 6,444評論 6 7
  • 張曉波在一片混沌中睜開沉重的眼皮,視線一開始無法聚焦,只能看到腦門兒上一盞明晃的白熾燈隱約的在他眼前散出光圈,一晃...
    貳_1d1a閱讀 3,597評論 0 0
  • 《5月 提筆寫作》鏈接目錄 6月 心靈修行 目錄(更新中) 關(guān)于馬盤的爭議(6月1日) 馬克思盤學(xué)習(xí)入門筆記(6月...
    晚晚inna閱讀 347評論 0 0
  • 本文圖片來自網(wǎng)絡(luò) 姨姥姥是姥姥的小妹妹,比姥姥小了整整10歲。然而在我看來,姨姥姥比姥姥老得多。 姨姥姥的家離姥姥...
    愛做美夢的懶貓閱讀 598評論 0 24

友情鏈接更多精彩內(nèi)容