NSLayoutAnchor實踐

iOS如果項目中不用xib或者storyboard的話刹孔,給view做約束一般都是用第三方庫Masonry赊级,為什么不用系統(tǒng)提供的AutoLayout呢?因為代碼太多不好用

  • 今天給大家介紹一個蘋果iOS9后更新的一個布局的好用的類 NSLayoutAnchor

分別用 NSLayoutConstraint NSLayoutAnchorMasonry來進行如下圖所示的布局

在進行布局的時候圈纺,我會分別講解下系統(tǒng) NSLayoutConstraint NSLayoutAnchor兩種布局的方法使用和參數(shù)說明
至于Masonry的使用 我就不做多介紹了(官方介紹的很清楚链沼,網(wǎng)上資料也不少)

WX20190509-105625@2x.png

第一種 NSLayoutConstraint

# NSLayoutConstraint 的核心布局方法
[NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];

上面代碼的白話文就是
紅色view 的 centerX 等于 self.view 的 centerX 1.0倍 加 0
如果還不是很明白的話 看下圖就一目了然了

WX20190509-110720@2x.png

接下來我們來講一下初始化方法中各個參數(shù)的意義:

item:        要布局的view

attribute: 是一個NSLayoutAttribute枚舉默赂,可以看到他的枚舉值有 left、right括勺、bottom放可、top 等

relatedBy:  是一個NSLayoutRelation枚舉,他的枚舉值有 lessThanOrEqual(小于等于)朝刊、equal(等于)、greaterThanOrEqual(大于等于)蜈缤, 指定 view1和接下來那個參數(shù) view2兩個視圖之
間的約束關系的

toItem:     第一個view的參照view 你要參照哪個view 這個就是哪個view(本例中的是self.view控制器view)

attribute:  和第二個參數(shù)一樣拾氓,是來表示第一個視圖對第二個視圖的
參考位置 ,上下左右 還是 center等

multiplier: 是來計算兩個視圖之間約束的倍數(shù)關系

constant:  是來計算兩個視圖之間約束的倍數(shù)關系的基礎上再加一些常量

下面看約束代碼

// 布局redView
    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTopMargin multiplier:1.0 constant:10];
    
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200];
    NSLayoutConstraint *heith = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200];
    
    //可以單個添加約束
    [self.view addConstraint:centerX];
    [self.view addConstraint:top];
    
    //也可以添加約束多個約束
    [self.redView addConstraints:@[width, heith]];
    
    
    
    // 布局blueView
    NSLayoutConstraint *centerX1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    NSLayoutConstraint *top1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
    
    NSLayoutConstraint *width1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeWidth multiplier:1.5 constant:0];
    NSLayoutConstraint *heith1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeWidth multiplier:1.5 constant:0];
    
    //iOS8 以后 NSLayoutConstraint 的類方法 也可以把約束添加到視圖上底哥,而且省掉了判斷添加到那個視圖上的問題咙鞍,避免了上面例子中因為視圖添加錯誤而導致的崩潰
    [NSLayoutConstraint activateConstraints:@[centerX1,top1, width1, heith1]];
NSLayoutConstraint添加約束注意事項

1.如果兩個視圖(也就是參數(shù) item 和 toItem)是父子關系,設置子控件的約束趾徽,約束添加到父控件上
2.如果兩個視圖(也就是參數(shù) item 和 toItem)是兄弟關系续滋,設置兩兄弟的約束,約束會添加到第一個共同的父控件上
3.如果兩個視圖(也就是參數(shù) item 和 toItem)是同一個視圖孵奶,約束會添加到自己上(一般給自己添加約束toItem為nil attribute為NSLayoutAttributeNotAnAttribute)
4.要給添加約束的view要設置translatesAutoresizingMaskIntoConstraints為NO 否則約束不生效

    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    // 要禁止 autoresize 意思就是遵循autoLayout拋棄原有設置的高度寬度等
    // 使用autolayout的視圖必須要設置該屬性
    redView.translatesAutoresizingMaskIntoConstraints = NO;

第二種 NSLayoutAnchor ios9以后

- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor;
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor;
- (NSLayoutConstraint *)constraintLessThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor;

/* These methods return an inactive constraint of the form thisAnchor = otherAnchor + constant.
 */
- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor constant:(CGFloat)c;
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor constant:(CGFloat)c;
- (NSLayoutConstraint *)constraintLessThanOrEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor constant:(CGFloat)c;

接下來我們來講一下使用方法

比如要給redView設置一個約束疲酌,如下

[self.redView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor];

上面代碼的意思為 紅色的view的centerX和self.view的centerX相等

下面看使用NSLayoutAnchor寫的約束代碼

    [self.redView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
    [self.redView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20].active = YES;
    [self.redView.widthAnchor constraintEqualToConstant:200].active = YES;
    [self.redView.heightAnchor constraintEqualToConstant:200].active = YES;
    
    [self.blueView.centerXAnchor constraintEqualToAnchor:self.redView.centerXAnchor].active = YES;
    [self.blueView.topAnchor constraintEqualToAnchor:self.redView.bottomAnchor constant:20].active = YES;
    [self.blueView.widthAnchor constraintEqualToAnchor:self.redView.widthAnchor multiplier:1.5].active = YES;
    [self.blueView.heightAnchor constraintEqualToAnchor:self.redView.heightAnchor multiplier:1.5].active = YES;
NSLayoutAnchor添加約束注意事項

1.要給添加約束的view要設置translatesAutoresizingMaskIntoConstraints為NO 否則約束不生效 自定義view的時候也要給子view設置此屬性,總之你要給哪個view設置Layout就要給哪個view設置此屬性為NO
2.active要設置為YES 這個是控制約束是否真正添加的開關 設置為NO的時候 約束失效

第三種 Masonry

直接上代碼了

    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_topMargin).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.height.equalTo(@200);
    }];
    
    [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.redView.mas_bottom).offset(20);
        make.centerX.equalTo(self.redView.mas_centerX);
        make.width.height.equalTo(self.redView).multipliedBy(1.5);
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末了袁,一起剝皮案震驚了整個濱河市朗恳,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌载绿,老刑警劉巖粥诫,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異崭庸,居然都是意外死亡怀浆,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門怕享,熙熙樓的掌柜王于貴愁眉苦臉地迎上來执赡,“玉大人,你說我怎么就攤上這事函筋〔缶粒” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵驻呐,是天一觀的道長灌诅。 經(jīng)常有香客問我芳来,道長,這世上最難降的妖魔是什么猜拾? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任即舌,我火速辦了婚禮,結(jié)果婚禮上挎袜,老公的妹妹穿的比我還像新娘顽聂。我一直安慰自己,他們只是感情好盯仪,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布紊搪。 她就那樣靜靜地躺著,像睡著了一般全景。 火紅的嫁衣襯著肌膚如雪耀石。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天爸黄,我揣著相機與錄音滞伟,去河邊找鬼。 笑死炕贵,一個胖子當著我的面吹牛梆奈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播称开,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼亩钟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鳖轰?” 一聲冷哼從身側(cè)響起径荔,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎脆霎,沒想到半個月后总处,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡睛蛛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年鹦马,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片忆肾。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡荸频,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出客冈,到底是詐尸還是另有隱情旭从,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站和悦,受9級特大地震影響退疫,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鸽素,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一褒繁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧馍忽,春花似錦棒坏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瓦呼,卻和暖如春喂窟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背吵血。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留偷溺,地道東北人蹋辅。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像挫掏,于是被迫代替她去往敵國和親侦另。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

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