#import "ViewController.h"
//define this constant if you want to use Masonry without the 'mas_' prefix
// 只要在導(dǎo)入Masonry主頭文件之前定義這個宏, 那么以后在使用Masonry框架中的屬性和方法的時候, 就可以省略mas_前綴
// 如果這個宏是在導(dǎo)入了Masonry.h之后定義, 那么無效
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
// 只要在導(dǎo)入Masonry主頭文件之前定義這個宏,那么就可以讓equalTo函數(shù)接收基本數(shù)據(jù)類型, 內(nèi)部會對基本數(shù)據(jù)類型進行包裝
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
@interface ViewController ()
/**
*/
@property (nonatomic,weak)UIView *redView;
/**
* <#注釋#>
*/
@property (nonatomic,weak)UIView *blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.創(chuàng)建控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// 2.將控件添加到父控件中
[self.view addSubview:redView];
self.redView = redView;
[self.view addSubview:blueView];
self.blueView = blueView;
// 3.禁用Autoresizing
redView.translatesAutoresizingMaskIntoConstraints = NO;
blueView.translatesAutoresizingMaskIntoConstraints = NO;
// 4.添加約束
// 4.1設(shè)置藍色的約束
[blueView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.top).offset(20); // y
make.left.equalTo(self.view.left).offset(20); // x
make.right.equalTo(self.view.right).offset(-20); // w
make.height.equalTo(50);
}];
// 4.2設(shè)置紅色的約束
[redView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(blueView.bottom).offset(20);
make.right.equalTo(blueView.right);
make.height.equalTo(blueView.height);
make.width.equalTo(blueView.width).multipliedBy(0.5);
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// makeConstraints: 每次都會添加新的約束
/*
[self.redView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.blueView.bottom).offset(100);
}];
*/
// updateConstraints: 專門用于更新約束的, 如果沒有約束會創(chuàng)建一個新的 如果有直接修改以前的
/*
[self.redView updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.blueView.bottom).offset(100);
}];
*/
// remakeConstraints: 清空約束, 刪除約束
[self.redView remakeConstraints:^(MASConstraintMaker *make) {
}];
}
- (void)demo2
{
// 1.創(chuàng)建控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 2.將控件添加到父控件中
[self.view addSubview:redView];
// 3.禁用Autoresizing
redView.translatesAutoresizingMaskIntoConstraints = NO;
// 4.添加約束
/*
// and / with masonry中的這兩個屬性沒有任何含義, 僅僅用于提高閱讀性
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).with.offset(20);
make.left.equalTo(self.view.mas_left).offset(20);
// make.width.equalTo(@100);
// make.height.equalTo(@100);
// make.width.height.equalTo(@100);
make.width.and.height.equalTo(@100);
}];
*/
// 去掉mas_前綴
//[redView mas_makeConstraints:^(MASConstraintMaker *make) {
[redView makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.view.mas_top).with.offset(20);
make.top.equalTo(self.view.top).with.offset(20);
make.left.equalTo(self.view.mas_left).offset(20);
// make.width.and.height.equalTo(@100);
// make.width.and.height.mas_equalTo(100);
make.width.and.height.equalTo(100);
}];
}
- (void)demo
{
// 1.創(chuàng)建控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 2.將控件添加到父控件中
[self.view addSubview:redView];
// 3.禁用Autoresizing
redView.translatesAutoresizingMaskIntoConstraints = NO;
// 4.添加約束
/*
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).offset(20);
make.left.equalTo(self.view.mas_left).offset(20);
make.bottom.equalTo(self.view.mas_bottom).offset(-20);
make.right.equalTo(self.view.mas_right).offset(-20);
}];
*/
/*
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(20);
make.left.equalTo(self.view).offset(20);
make.bottom.equalTo(self.view).offset(-20);
make.right.equalTo(self.view).offset(-20);
}];
*/
/*
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(20);
make.left.offset(20);
make.bottom.offset(-20);
make.right.offset(-20);
}];
*/
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.insets(UIEdgeInsetsMake(20, 20, 20, 20));
}];
}
@end
Masonry
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赢乓,“玉大人忧侧,你說我怎么就攤上這事∨朴螅” “怎么了蚓炬?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長躺屁。 經(jīng)常有香客問我肯夏,道長,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任驯击,我火速辦了婚禮烁兰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘余耽。我一直安慰自己缚柏,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布碟贾。 她就那樣靜靜地躺著币喧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪袱耽。 梳的紋絲不亂的頭發(fā)上杀餐,一...
- 文/蒼蘭香墨 我猛地睜開眼洪唐,長吁一口氣:“原來是場噩夢啊……” “哼钻蹬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起凭需,我...
- 正文 年R本政府宣布,位于F島的核電站漆腌,受9級特大地震影響畔派,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一捌治、第九天 我趴在偏房一處隱蔽的房頂上張望岗钩。 院中可真熱鬧,春花似錦肖油、人聲如沸兼吓。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽视搏。三九已至,卻和暖如春县袱,著一層夾襖步出監(jiān)牢的瞬間浑娜,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 前言 純手寫代碼一共經(jīng)歷了三個時期响驴,這里我就不介紹了,隨著iPhone6的出現(xiàn)那伐,蘋果手機的尺寸也就不在單一踏施。為了讓...
- ] *** Assertion failure in -[MASViewConstraint addConstra...
- 用cocoapods導(dǎo)入了Masonry在.pch文件中引入時卻出現(xiàn)了下面的問題 This happened to...