欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年02月13日 16:41:20   作者:總李寫代碼  
本篇文章主要介紹了深入理解IOS控件布局之Masonry布局框架 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前言:

回想起2013年做iOS開發(fā)的時(shí)候,那時(shí)候并沒有采用手寫布局代碼的方式,而是采用xib文件來編寫,如果使用純代碼方式是基于window的size(320,480)計(jì)算出一個(gè)相對(duì)位置進(jìn)行布局,那個(gè)時(shí)候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í)使用這個(gè)布局框架對(duì)我來說至關(guān)重要,它大大提高了開發(fā)效率而且最近使用起來很多語法和Android有很大的相似之處。

什么是Masonry?

Masonry是一個(gè)輕量級(jí)的布局框架,擁有自己的描述語法,采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動(dòng)布局、簡(jiǎn)潔明了、 并具有高可讀性、 而且同時(shí)支持 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提供了三個(gè)函數(shù)方法

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

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

3.)具體舉例

  比如一個(gè)往父控件中添加一個(gè)上下左右與父控件間距為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);
  }];

等價(jià)于

  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);
  }];

也可以簡(jiǎn)化為下面這種

  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));
  }];

又等價(jià)于

  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的時(shí)候很容易混淆他們兩個(gè),今天特意分析一下他們的區(qū)別。mas_equalTo是一個(gè)MACRO,比較的是值,equalTo比較的是id類型。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論