溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

深入理解IOS控件布局之Masonry布局框架

發(fā)布時間:2020-08-31 15:34:50 來源:腳本之家 閱讀:116 作者:總李寫代碼 欄目:移動開發(fā)

前言:

回想起2013年做iOS開發(fā)的時候,那時候并沒有采用手寫布局代碼的方式,而是采用xib文件來編寫,如果使用純代碼方式是基于window的size(320,480)計(jì)算出一個相對位置進(jìn)行布局,那個時候windows的size是固定不變的,隨著iphone5的發(fā)布,windows的size(320,568)也發(fā)生了變化,而采用autoresizingMask的方式進(jìn)行適配,到后來iphone 6之后windows size的寬度也隨之變化,開始拋棄autoresizingMask改用autolayout了,使用autolayout進(jìn)行適配我也是最近重新做iOS開發(fā)才接觸的,公司使用Masonry框架進(jìn)行布局適配。所以學(xué)習(xí)使用這個布局框架對我來說至關(guān)重要,它大大提高了開發(fā)效率而且最近使用起來很多語法和Android有很大的相似之處。

什么是Masonry?

Masonry是一個輕量級的布局框架,擁有自己的描述語法,采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動布局、簡潔明了、 并具有高可讀性、 而且同時支持 iOS 和 Max OS X。

如何使用?

1.)引入頭文件

我這里是在全局引用pch文件中引用的

#import "Masonry.h"

2.)基本語法

Masonry提供的屬性

  • @property (nonatomic, strong, readonly) MASConstraint *left;//左側(cè)
  • @property (nonatomic, strong, readonly) MASConstraint *top;//上側(cè)
  • @property (nonatomic, strong, readonly) MASConstraint *right;//右側(cè)
  • @property (nonatomic, strong, readonly) MASConstraint *bottom;//下側(cè)
  • @property (nonatomic, strong, readonly) MASConstraint *leading;//首部
  • @property (nonatomic, strong, readonly) MASConstraint *trailing;//尾部
  • @property (nonatomic, strong, readonly) MASConstraint *width;//寬
  • @property (nonatomic, strong, readonly) MASConstraint *height;//高
  • @property (nonatomic, strong, readonly) MASConstraint *centerX;//橫向居中
  • @property (nonatomic, strong, readonly) MASConstraint *centerY;//縱向居中
  • @property (nonatomic, strong, readonly) MASConstraint *baseline;//文本基線

Masonry提供了三個函數(shù)方法

  • - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增約束
  • - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新約束
  • - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有約束,只會保留最新的約束

我們根據(jù)不同的使用場景來選擇使用不同的函數(shù)方法。

3.)具體舉例

  比如一個往父控件中添加一個上下左右與父控件間距為50的子視圖

添加約束

  UIView *tempView=[[UIView alloc]init];
  tempView.backgroundColor=[UIColor greenColor];
  [self.view addSubview:tempView];
  
  [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(50);
    make.right.mas_equalTo(-50);
    make.top.mas_equalTo(50);
    make.bottom.mas_equalTo(-50);
  }];

等價于

  UIView *tempView=[[UIView alloc]init];
  tempView.backgroundColor=[UIColor greenColor];
  [self.view addSubview:tempView];
  
  [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view.mas_left).offset(50);
    make.right.equalTo(self.view.mas_right).offset(-50);
    make.top.equalTo(self.view.mas_top).offset(50);
    make.bottom.equalTo(self.view.mas_bottom).offset(-50);
  }];

也可以簡化為下面這種

  UIView *tempView=[[UIView alloc]init];
  tempView.backgroundColor=[UIColor greenColor];
  [self.view addSubview:tempView];
  
  [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50));
  }];

又等價于

  UIView *tempView=[[UIView alloc]init];
  tempView.backgroundColor=[UIColor greenColor];
  [self.view addSubview:tempView];
  
  [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
  }];

更新約束

  [tempView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(50);
    make.right.mas_equalTo(-50);
    make.top.mas_equalTo(100);
    make.bottom.mas_equalTo(-100);
  }];

清除之前的約束保留最新的

  [tempView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(100);
    make.right.mas_equalTo(-100);
    make.top.mas_equalTo(100);
    make.bottom.mas_equalTo(-100);
  }];

特別注意:

聲明約束必須在視圖添加到父試圖上面之后調(diào)用。

4.)mas_equalTo與equalTo

上面的舉例中分別使用了mas_equalTo和equalTo達(dá)到了同樣的效果,我在剛開始使用Masonry的時候很容易混淆他們兩個,今天特意分析一下他們的區(qū)別。mas_equalTo是一個MACRO,比較的是值,equalTo比較的是id類型。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI