初步學(xué)習(xí), 簡(jiǎn)單記錄一下不同約束方法的使用.
首先, Masonry是一個(gè)第三個(gè)框架, 我們可以調(diào)用它已經(jīng)定義好的約束方法, 這里使用Cocoapods導(dǎo)入該框架
platform :ios, '8.0'
target 'MasoryJJ' do
pod 'Masonry'
end
然后, 在工程中到入頭文件
頭文件 #import <Masonry.h>
我在查閱資料的時(shí)候, 發(fā)現(xiàn)了兩個(gè)宏定義:
#define MAS_SHORTHAND_GLOBALS
添加該宏赊豌,那么mas_equalTo和equalTo就沒(méi)有區(qū)別,注意:這個(gè)宏一定要添加到#import "Masonry.h"前面
#define MAS_SHORTHAND
如果添加了上面的宏赖欣,mas_width也可以寫成width
mas_width : 是一個(gè)屬性值, 當(dāng)做equalTo的參數(shù)
width : make對(duì)象的一個(gè)屬性
* 我驗(yàn)證后的效果是, 第二個(gè)宏是有效果的, 第一個(gè)沒(méi)有得到證明 , 繼續(xù)努力
下面為實(shí)現(xiàn)效果代碼
***約束一
UIView *backView = [[UIView alloc] init];
backView.backgroundColor = [UIColor colorWithRed:0.429 green:0.574 blue:1.000 alpha:1.000];
[self.view addSubview:backView];
[backView mas_makeConstraints:^(MASConstraintMaker *make) {
//大小
make.size.mas_equalTo(CGSizeMake(300, 200));
//坐標(biāo)
make.centerX.equalTo(self.view);
make.top.equalTo(self.view).with.offset(30);
}];
*** 約束二
UIView *leftView = [[UIView alloc] init];
leftView.backgroundColor = [UIColor redColor];
[self.view addSubview:leftView];
[leftView mas_makeConstraints:^(MASConstraintMaker *make) {
# 這里用的是 mas_equalTo
make.left.mas_equalTo(backView.mas_left).with.offset(10);
make.top.mas_equalTo(backView.mas_top).with.offset(10);
make.right.mas_equalTo(backView.centerX).with.offset(-10);
make.bottom.mas_equalTo(backView.centerY).with.offset(-10);
}];
*** 約束三
UIView *rightView = [[UIView alloc]init];
rightView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:rightView];
[rightView mas_makeConstraints:^(MASConstraintMaker *make) {
# 這里是添加宏定義后, 使用 equalTo (沒(méi)有添加宏定義也可以使用)
make.right.equalTo(backView.mas_right).with.offset(-10);
make.left.equalTo(backView.mas_centerX).with.offset(10);
make.top.equalTo(backView.mas_top).with.offset(10);
make.bottom.equalTo(backView.centerY).with.offset(-10);
}];
*** 約束三
UIView *leftView2 = [[UIView alloc] init];
leftView2.backgroundColor = [UIColor orangeColor];
[self.view addSubview:leftView2];
[leftView2 mas_makeConstraints:^(MASConstraintMaker *make) {
#這里是添加宏定義后, mas_height == height
make.right.equalTo(leftView.right);
make.left.equalTo(leftView.left);
make.top.equalTo(leftView.bottom).with.offset(10);
make.height.equalTo(leftView.height);
}];
// //更新約束
// [leftView2 updateConstraints:^(MASConstraintMaker *make) {
//
// //???
// make.right.equalTo(backView.right).offset(-10);
//
// }];
//
// //刪除指定控件的約束
// [leftView2 remakeConstraints:^(MASConstraintMaker *make) {
//
// }];
***約束四
UIView *botton = [[UIView alloc] init];
botton.backgroundColor = [UIColor greenColor];
[self.view addSubview:botton];
[botton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(backView.left);
make.top.equalTo(backView.bottom).offset(20);
make.width.equalTo(backView.width);
make.height.equalTo(backView.height);
}];
*** 約束五
UIView *bottonIn = [[UIView alloc] init];
bottonIn.backgroundColor = [UIColor blueColor];
[botton addSubview:bottonIn];
[bottonIn mas_makeConstraints:^(MASConstraintMaker *make) {
# 相對(duì)于父視圖來(lái)說(shuō)的內(nèi)邊距設(shè)置
make.edges.mas_offset(UIEdgeInsetsMake(10, 10, 10, 10));
}];