iOS自動布局 -masonry

需要適配的手機的尺寸

1.iphone4 4s (320 480)
2.iphone5 5s (320 568)
3.iphone6 6s (375x667)
4.iphone6p 6sp (414x736)
適配不同的屏幕尺寸常用的有三種形式

1.xib自動布局
2.使用NSLayoutConstraint類代碼創(chuàng)建(包括第三方類庫Masonry的使用)
3.使用VFM布局

今天我們了解的是Masonry第三方布局的時候用
VFM的使用可以參考http://www.reibang.com/p/dfc11861cc85里面有比較詳細(xì)的介紹

masonry下載地址:https://github.com/SnapKit/Masonry

一 .查看mastory的屬性
1.查看mastery的屬性

@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute  *mas_baseline;

和NSLayoutAttrubute對比

1418956465427751 (1).jpg

常用的3個方法
1.mas_makeConstraints 只負(fù)責(zé)添加新增約束面褐,Autolayout中不能同時存在兩條針對于同一對象的約束,否則會報錯

 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
  1. mas_updateConstraints 針對上面的情況取胎,會更新在block中出現(xiàn)的約束展哭,確保不會出現(xiàn)兩個相同的約束
  - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

3.mas_remakeConstraints 會清除之前的所有約束湃窍,僅保留最新的約束

- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

使用方法示例

1、 居中顯示一個View

// 居中顯示一個view
UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
//在使用autolayout之前一定要先將試圖添加到superview上 否則會報錯
[self.view addSubview:view1];
// 用masonry約束布局
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    // 設(shè)置view1居中
    make.center.equalTo(self.view);
    // 設(shè)置view的寬 高
    make.size.mas_equalTo(CGSizeMake(200, 200));
}];

2.讓一個View小于其superView(邊界10)

方法一:官方文檔提供方式

UIView *view2 = [[UIView alloc]init];
view2.backgroundColor = [UIColor cyanColor];
[self.view addSubview:view2];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view.mas_top).with.offset(padding.top);
    make.left.equalTo(self.view.mas_left).with.offset(padding.left);
    make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding.bottom);
    make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
}];

方法二:官方文檔方式:

//讓一個View小于其superView(邊界15)
 UIView *view2 = [[UIView alloc]init];
 view2.backgroundColor = [UIColor cyanColor];
[self.view addSubview:view2];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view).insets(padding);
}];

方法三:

//讓一個View小于其superView(邊界15)
UIView *view2 = [[UIView alloc]init];
view2.backgroundColor = [UIColor cyanColor];
[self.view addSubview:view2];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).with.offset(padding.top);
    make.left.equalTo(self.view).with.offset(padding.left);
    make.bottom.equalTo(self.view).with.offset(-padding.bottom);
    make.right.equalTo(self.view).with.offset(-padding.right);
}];

方法四:

//讓一個View小于其superView(邊界15)
UIView *view2 = [[UIView alloc]init];
view2.backgroundColor = [UIColor cyanColor];
[self.view addSubview:view2];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
   make.top.left.bottom.and.right.equalTo(self.view).width.insets(padding);

}];

以上四中方法的實現(xiàn)效果是一樣的其中要注意上面那個是with不是width如果輸入錯誤會出現(xiàn)
如下錯誤

 Attributes should be chained before defining the constraint relation

with沒有什么語法意義只是讓語法好看匪傍,width是表示寬度的意思

3.實現(xiàn)兩個視圖2等分平均分配居中

// 讓2個view平分

UIView *view3 = [[UIView alloc]init];
view3.backgroundColor = [UIColor redColor];
[self.view addSubview:view3];

UIView *view4 = [[UIView alloc]init];
view4.backgroundColor  = [UIColor greenColor];
[self.view addSubview:view4];

[view3  mas_makeConstraints:^(MASConstraintMaker *make) {
    
    make.centerY.mas_equalTo(self.view.mas_centerY);
    make.left.equalTo(self.view.mas_left).with.offset(10);
    make.right.equalTo(view4.mas_left).with.offset(-10);
    make.height.mas_equalTo(@150);
    make.width.equalTo(view4);
    
} ];

[view4 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.mas_equalTo(self.view.mas_centerY);
    make.left.equalTo(view3.mas_right).with.offset(10);
    make.right.equalTo(self.view.mas_right).with.offset(-10);
    make.height.equalTo(view3);
    make.width.equalTo(view3);
}];

頁面效果:

Simulator Screen Shot 2016年4月14日 14.51.31.png

參考文檔:

1.https://github.com/SnapKit/Masonry
2.http://www.cocoachina.com/ios/20141219/10702.html

歡迎關(guān)注我們的微信公眾號您市,大家一起學(xué)習(xí)交流。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末役衡,一起剝皮案震驚了整個濱河市茵休,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌映挂,老刑警劉巖泽篮,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異柑船,居然都是意外死亡帽撑,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門鞍时,熙熙樓的掌柜王于貴愁眉苦臉地迎上來亏拉,“玉大人,你說我怎么就攤上這事逆巍〖疤粒” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵锐极,是天一觀的道長笙僚。 經(jīng)常有香客問我,道長灵再,這世上最難降的妖魔是什么肋层? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮翎迁,結(jié)果婚禮上栋猖,老公的妹妹穿的比我還像新娘。我一直安慰自己汪榔,他們只是感情好蒲拉,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著痴腌,像睡著了一般雌团。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上士聪,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天辱姨,我揣著相機與錄音,去河邊找鬼戚嗅。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的懦胞。 我是一名探鬼主播替久,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼躏尉!你這毒婦竟也來了蚯根?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤胀糜,失蹤者是張志新(化名)和其女友劉穎颅拦,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體教藻,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡距帅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了括堤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片碌秸。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖悄窃,靈堂內(nèi)的尸體忽然破棺而出讥电,到底是詐尸還是另有隱情,我是刑警寧澤轧抗,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布恩敌,位于F島的核電站,受9級特大地震影響横媚,放射性物質(zhì)發(fā)生泄漏纠炮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一分唾、第九天 我趴在偏房一處隱蔽的房頂上張望抗碰。 院中可真熱鬧,春花似錦绽乔、人聲如沸弧蝇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽看疗。三九已至,卻和暖如春睦授,著一層夾襖步出監(jiān)牢的瞬間两芳,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工去枷, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留怖辆,地道東北人是复。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像竖螃,于是被迫代替她去往敵國和親淑廊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359

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