iOS - Masonry使用中的一些整理

[置頂]iOS - Masonry使用中的一些整理

標(biāo)簽:iOS資源大全iOS常用方法iOS學(xué)習(xí)資料Masonry使用

2016-07-19 20:2211213人閱讀評(píng)論(0)收藏舉報(bào)

分類(lèi):

iOS的開(kāi)發(fā)(484)

目錄(?)[+]

個(gè)人喜歡用純代碼寫(xiě)東西,其中用到最多的就是砌體,我整理一些使用過(guò)程中一些點(diǎn),方便以后使用。(基本的語(yǔ)法就不說(shuō)了)

首先說(shuō)幾點(diǎn):

我一般將數(shù)值類(lèi)型的約束用mas_equalTo,而相對(duì)于某個(gè)控件,或者某個(gè)控件的某個(gè)約束,我會(huì)使用equalTo,如:

make.size.mas_equalTo(CGSizeMake(100, 100));

make.center.equalTo(weakSelf.view);

setNeedsLayout:告知頁(yè)面需要更新,但是不會(huì)立刻開(kāi)始更新。執(zhí)行后會(huì)立刻調(diào)用layoutSubviews

layoutIfNeeded。:告知頁(yè)面布局立刻更新。如此setNeedsLayout希望立刻生成新的框架需要調(diào)用此方法,利用這點(diǎn)一般布局動(dòng)畫(huà)可以在更新布局后直接使用這個(gè)方法讓動(dòng)畫(huà)生效

layoutSubviews:系統(tǒng)重寫(xiě)布局

setNeedsUpdateConstraints:告知需要更新約束,但是不會(huì)立刻開(kāi)始

updateConstraintsIfNeeded:告知立刻更新約束

updateConstraints:系統(tǒng)更新約束

- (void)updateViewConstraintsViewController的查看在更新視圖布局時(shí),會(huì)先調(diào)用ViewController的updateViewConstraints方法。我們可以通過(guò)重寫(xiě)這個(gè)方法去更新當(dāng)前查看的內(nèi)部布局,而不用再繼承這個(gè)查看去重寫(xiě)-updateConstraints方法我們?cè)谥貙?xiě)這個(gè)方法時(shí),務(wù)必要調(diào)用super或者調(diào)用當(dāng)前View的-updateConstraints方法。

視圖居中顯示

// 防止block中的循環(huán)引用__weaktypeof(self) weakSelf =self;UIView* view? ? ? ? = [UIViewnew];view.backgroundColor= [UIColorbrownColor];[self.viewaddSubview:view];//使用mas_makeConstraints添加約束[view mas_makeConstraints:^(MASConstraintMaker *make) {// 添加大小約束(make就是要添加約束的控件view)make.size.mas_equalTo(CGSizeMake(200,200));// 添加居中約束(居中方式與self相同)make.center.equalTo(weakSelf.view);}];

兩個(gè)視圖等寬高邊距

UIView* blackView? ? ? = [UIViewnew];blackView.backgroundColor= [UIColorblackColor];[self.viewaddSubview:blackView];[blackView mas_makeConstraints:^(MASConstraintMaker *make) {//添加約束大小make.size.mas_equalTo(CGSizeMake(100,100));//在 左,上 添加約束 (左、上約束都是20)make.left.and.top.mas_equalTo(20);}];UIView* grayView? ? ? ? = [UIViewnew];grayView.backgroundColor= [UIColorlightGrayColor];[self.viewaddSubview:grayView];[grayView mas_makeConstraints:^(MASConstraintMaker *make) {// 大小、上邊距約束與黑色view相同make.size.and.top.equalTo(blackView);// 添加右邊距約束(這里的間距是有方向性的,左、上邊距約束為正數(shù),右、下邊距約束為負(fù)數(shù))make.right.mas_equalTo(-20);}];

鍵盤(pán)彈出和收回

- (void)dealloc {[[NSNotificationCenterdefaultCenter] removeObserver:self];}- (void)viewDidLoad {[superviewDidLoad];// Do any additional setup after loading the view.__weaktypeof(self) weakSelf =self;_textField? ? ? ? ? ? ? ? = [UITextFieldnew];_textField.backgroundColor= [UIColorredColor];[self.viewaddSubview:_textField];[_textField mas_makeConstraints:^(MASConstraintMaker *make) {//left,right,centerx,y? 不能共存只能有其二make.left.mas_equalTo(20);//? ? ? ? make.right.mas_equalTo(-60);make.centerX.equalTo(weakSelf.view);make.height.mas_equalTo(40);make.bottom.mas_equalTo(0);}];// 注冊(cè)鍵盤(pán)通知[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotificationobject:nil];[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotificationobject:nil];}- (void)keyboardWillChangeFrameNotification:(NSNotification*)notification {// 獲取鍵盤(pán)基本信息(動(dòng)畫(huà)時(shí)長(zhǎng)與鍵盤(pán)高度)NSDictionary*userInfo = [notification userInfo];CGRectrect = [userInfo[UIKeyboardFrameBeginUserInfoKey]CGRectValue];CGFloatkeyboardHeight? =CGRectGetHeight(rect);CGFloatkeyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];// 修改下邊距約束[_textField mas_updateConstraints:^(MASConstraintMaker *make) {make.bottom.mas_equalTo(-keyboardHeight);}];// 更新約束[UIViewanimateWithDuration:keyboardDuration animations:^{[self.viewlayoutIfNeeded];}];}- (void)keyboardWillHideNotification:(NSNotification*)notification {// 獲得鍵盤(pán)動(dòng)畫(huà)時(shí)長(zhǎng)NSDictionary*userInfo? = [notification userInfo];CGFloatkeyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];// 修改為以前的約束(距下邊距0)[_textField mas_updateConstraints:^(MASConstraintMaker *make) {make.bottom.mas_equalTo(0);}];// 更新約束[UIViewanimateWithDuration:keyboardDuration animations:^{[self.viewlayoutIfNeeded];}];}- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {[supertouchesBegan:touches withEvent:event];[self.viewendEditing:YES];}

三控件等寬間距

方法一:

array 的 mas_distributeViewsAlongAxis withFixedSpacing變化的是控件長(zhǎng)度或?qū)挾?/p>

定義一個(gè)存放三個(gè)控件的數(shù)組NSArray *array;

array = @[greenView,redView,blueView];

直接調(diào)用下面的方法:

- (void)getHorizontalone{//方法一,array 的 mas_distributeViewsAlongAxis/**

*? 多個(gè)控件固定間隔的等間隔排列,變化的是控件的長(zhǎng)度或者寬度值

*

*? @param axisType? ? ? ? 軸線方向

*? @param fixedSpacing? ? 間隔大小

*? @param leadSpacing? ? 頭部間隔

*? @param tailSpacing? ? 尾部間隔

*///? ? MASAxisTypeHorizontal? 水平//? ? MASAxisTypeVertical? ? 垂直[arrayList mas_distributeViewsAlongAxis:MASAxisTypeHorizontalwithFixedSpacing:20leadSpacing:5tailSpacing:5];[arrayList mas_makeConstraints:^(MASConstraintMaker *make) {make.top.mas_equalTo(60);make.height.mas_equalTo(100);}];

}

方法二:

array de mas_distributeViewsAlongAxis withFixedItemLength控件的大小不變,變化的是間隙

- (void)getVertical{/**

*? 多個(gè)固定大小的控件的等間隔排列,變化的是間隔的空隙

*

*? @param axisType? ? ? ? 軸線方向

*? @param fixedItemLength 每個(gè)控件的固定長(zhǎng)度或者寬度值

*? @param leadSpacing? ? 頭部間隔

*? @param tailSpacing? ? 尾部間隔

*/[arrayList mas_distributeViewsAlongAxis:MASAxisTypeVerticalwithFixedItemLength:60leadSpacing:40tailSpacing:10];[arrayList mas_makeConstraints:^(MASConstraintMaker *make) {//? ? ? ? make.top.mas_equalTo(100);//? ? ? ? make.height.mas_equalTo(100);make.left.mas_equalTo(20);make.right.mas_equalTo(-20);}];

}

倆以上都方法在NSArray+MASAdditions中

方法三:設(shè)置直接multiplier實(shí)現(xiàn)等間距

for(NSUIntegeri =0; i <4; i++) {UIView*itemView = [selfgetItemViewWithIndex:i];[_containerView addSubview:itemView];[itemView mas_makeConstraints:^(MASConstraintMaker *make) {make.width.and.height.equalTo(@(ITEM_SIZE));make.centerY.equalTo(_containerView.mas_centerY);make.centerX.equalTo(_containerView.mas_right).multipliedBy(((CGFloat)i +1) / ((CGFloat)ITEM_COUNT +1));}];}

方法四:利用透明等寬度的SpaceView實(shí)現(xiàn)等間距

UIView*lastSpaceView? ? ? = [UIViewnew];lastSpaceView.backgroundColor= [UIColorgreenColor];[_containerView1 addSubview:lastSpaceView];[lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.and.top.and.bottom.equalTo(_containerView1);}];for(NSUIntegeri =0; i < ITEM_COUNT; i++) {UIView*itemView = [selfgetItemViewWithIndex:i];[_containerView1 addSubview:itemView];[itemView mas_makeConstraints:^(MASConstraintMaker *make) {make.height.and.width.equalTo(@(ITEM_SIZE));make.left.equalTo(lastSpaceView.mas_right);make.centerY.equalTo(_containerView1.mas_centerY);}];UIView*spaceView? ? ? ? = [UIViewnew];spaceView.backgroundColor= [UIColorgreenColor];[_containerView1 addSubview:spaceView];[spaceView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(itemView.mas_right).with.priorityHigh();// 降低優(yōu)先級(jí),防止寬度不夠出現(xiàn)約束沖突make.top.and.bottom.equalTo(_containerView1);make.width.equalTo(lastSpaceView.mas_width);}];lastSpaceView = spaceView;}[lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {make.right.equalTo(_containerView1.mas_right);}];

動(dòng)態(tài)改變字體寬度

和面方法4一樣,利用spaceView來(lái)實(shí)現(xiàn)

UIView* bgView? ? ? = [[UIViewalloc]init];bgView.backgroundColor= [UIColoryellowColor];[self.viewaddSubview:bgView];[bgView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.and.right.mas_equalTo(0);make.top.mas_equalTo(@100);make.height.mas_equalTo(@100);}];listText = @[@"北京",@"地大吳波啊",@"你大爺",@"我們的愛(ài)哎哎"];UIView*lastSpaceView =nil;for(inti =0; i < listText.count;? i ++){UILabel* label = [UILabelnew];label.text= listText[i];label.backgroundColor= RANDOMCOLOR;[bgView addSubview:label];


UIView* lineView? ? ? ? = [UIViewnew];lineView.backgroundColor= [UIColorredColor];[bgView addSubview:lineView];[label mas_makeConstraints:^(MASConstraintMaker *make) {make.top.bottom.mas_equalTo(0);if(lastSpaceView){NSLog(@"存在 lastView");make.left.equalTo(lastSpaceView.mas_right).mas_offset(@20);}else{NSLog(@"不存在存在 lastView");make.left.equalTo(bgView.mas_left);}make.height.equalTo(bgView);}];lastSpaceView = label;[lineView mas_makeConstraints:^(MASConstraintMaker *make) {make.top.and.bottom.mas_equalTo(0);make.width.mas_equalTo(1);make.left.mas_equalTo(label.mas_right).mas_offset(@10);}];}



效果圖:

父視圖的高度,是里面?zhèn)z控制高度的和

UIView* bgView? ? ? = [UIViewnew];bgView.backgroundColor= [UIColorpurpleColor];[self.viewaddSubview:bgView];UILabel* titleLab? ? ? ? = [UILabelnew];titleLab.backgroundColor= [UIColorredColor];titleLab.textAlignment=NSTextAlignmentCenter;titleLab.font= [UIFontsystemFontOfSize:15.f];titleLab.text=@"曹操——《短歌行》";[bgView addSubview:titleLab];UILabel* contentLab? ? ? ? = [UILabelnew];contentLab.numberOfLines=0;contentLab.textAlignment=NSTextAlignmentCenter;contentLab.backgroundColor= [UIColorbrownColor];contentLab.font= [UIFontsystemFontOfSize:13.f];contentLab.text=@" 對(duì)酒當(dāng)歌,人生幾何? 譬如朝露,去日苦多。\n 慨當(dāng)以慷,憂思難忘。 何以解憂?唯有杜康。\n 青青子衿,悠悠我心。 但為君故,沉吟至今。\n 呦呦鹿鳴,食野之蘋(píng)。 我有嘉賓,鼓瑟吹笙。\n 明明如月,何時(shí)可掇? 憂從中來(lái),不可斷絕。\n 越陌度阡,枉用相存。 契闊談宴,心念舊恩。\n 月明星稀,烏鵲南飛。 繞樹(shù)三匝,何枝可依?\n 山不厭高,海不厭深。 周公吐哺,天下歸心。";[bgView addSubview:contentLab];//思路: 父視圖的上間距等于title的上間距,父視圖的下間距等于content的下間距__weaktypeof(self) weakSelf =self;[bgView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.mas_offset(@30);make.right.mas_offset(@-30);make.centerY.equalTo(weakSelf.view);}];[titleLab mas_makeConstraints:^(MASConstraintMaker *make) {make.left.top.right.mas_equalTo(@0);}];[contentLab mas_makeConstraints:^(MASConstraintMaker *make) {make.left.right.mas_equalTo(@0);make.top.equalTo(titleLab.mas_bottom).mas_offset(@10);make.bottom.equalTo(bgView);}];

效果圖:

以后慢慢更新,記錄方便以后使用

文/棟飛

//一些扒的別人的記錄

。自適應(yīng)布局允許將寬度或高度設(shè)置為固定值如果你想要給視圖一個(gè)最小或最大值,你可以這樣:

//width >= 200 && width <= 400make.width.greaterThanOrEqualTo(@200);make.width.lessThanOrEqualTo(@400)

約束的優(yōu)先級(jí)

.priority允許你指定一個(gè)精確的優(yōu)先級(jí),數(shù)值越大優(yōu)先級(jí)越高最高1000.

.priorityHigh等價(jià)于UILayoutPriorityDefaultHigh。優(yōu)先級(jí)值為750.

.priorityMedium介紹高優(yōu)級(jí)和低優(yōu)先級(jí)之間,優(yōu)先級(jí)值在250?750之間間

.priorityLow等于UILayoutPriorityDefaultLow,優(yōu)先級(jí)值為250。

優(yōu)先級(jí)可以在約束的尾部添加:

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();

make.top.equalTo(label.mas_top).with.priority(600);

中心中心

//使centerX和centerY = button1

make.center.equalTo(button1)

//使centerX = superview.centerX - 5,centerY =superview.centerY + 10make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

指定寬度為父視圖的1/4。

make.width.equalTo(superview).multipliedBy(0.25);

文/魏同學(xué)(簡(jiǎn)書(shū)作者)

原文鏈接:HTTP://www.lxweimin.com/p/a24dd8638d28

著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡(jiǎn)書(shū)作者”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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