Masonry學習
常用方法
設定view的位置
- (void)initViews{
UIView *view = [UIView new];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(view.superview).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
/* 等價於
make.top.equalTo(sv).with.offset(10);
make.left.equalTo(sv).with.offset(10);
make.bottom.equalTo(sv).with.offset(-10);
make.right.equalTo(sv).with.offset(-10);
*/
/* 也等價於
make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
*/
}];
}
設定view的size
make.size.mas_equalTo(CGSizeMake(40, 40));
更新刪除view的約束
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
/*
mas_makeConstraints 只負責新增約束 Autolayout不能同時存在兩條針對於同一物件的約束 否則會報錯
mas_updateConstraints 針對上面的情況 會更新在block中出現的約束 不會導致出現兩個相同約束的情況
mas_remakeConstraints 則會清除之前的所有約束 僅保留最新的約束
三種函式善加利用 就可以應對各種情況了
*/
mas_equalTo()和equalTo()的區別
mas_equalTo支援的型別更多
#define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))
#define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
#define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
#define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__)))
一個view
居中
///* 居中
make.size.mas_equalTo(CGSizeMake(100, 60));
make.centerY.equalTo(view.superview.mas_centerY);
make.centerX.equalTo(view.superview.mas_centerX);
// */
多個view
大小相同
make.width.equalTo(v1.mas_width);
make.height.equalTo(v1.mas_height);
首尾相連
//與v2上下間距為10
make.bottom.mas_equalTo(v2.mas_top).offset(-10);
//與v1左右間距為10
make.left.equalTo(v1.mas_right).offset(10);
同一垂直中線
//與v1的x中心點在同一直線
make.centerX.equalTo(v1.mas_centerX);
示例
- (void)towView{
UIView *v1 = [UIView new];
v1.backgroundColor = [UIColor redColor];
[self.view addSubview:v1];
UIView *v2 = [UIView new];
v2.backgroundColor = [UIColor blackColor];
[self.view addSubview:v2];
[v1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(200, 120));
make.top.mas_equalTo(v1.superview).offset(74);
//與v2上下間距為10
make.bottom.mas_equalTo(v2.mas_top).offset(-10);
make.centerX.equalTo(v1.superview.mas_centerX);
}];
[v2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(250, 30));
make.centerX.equalTo(v1.mas_centerX);
/*和v1的寬高相等
make.width.equalTo(v1.mas_width);
make.height.equalTo(v1.mas_height);
//*/
make.top.mas_equalTo(v1.mas_bottom).offset(10);
make.bottom.mas_equalTo(v2.superview).offset(-10);
}];
}
九宮格排序
- (void)squaredUp{
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i<14; i++) {
[array addObject:[NSString stringWithFormat:@"%d",i]];
}
__block UIView *lastV = nil;
int list = 4;
int gap = 10;
for (int i = 0; i<array.count; i++) {
UILabel *label = [UILabel new];
label.text = array[i];
[self.view addSubview:label];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor magentaColor];
[label sizeToFit];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
if (lastV) {
make.width.equalTo(lastV.mas_width);
}else
make.size.mas_equalTo(CGSizeMake((self.view.frame.size.width - gap*(list + 1))/list, 20));
if (i%list == 0) {
make.left.equalTo(label.superview).offset(gap);
}
else{
make.left.equalTo(lastV.mas_right).offset(gap);
}
if (i%list == (list -1)) {
make.right.equalTo(label.superview).offset(-gap);
}
int top = 64+(i/list+1)*gap+(i/list*label.frame.size.height);
make.top.equalTo(label.superview).offset(top);
lastV = label;
}];
}
}
設定view的位置
- (void)initViews{
UIView *view = [UIView new];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(view.superview).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
/* 等價於
make.top.equalTo(sv).with.offset(10);
make.left.equalTo(sv).with.offset(10);
make.bottom.equalTo(sv).with.offset(-10);
make.right.equalTo(sv).with.offset(-10);
*/
/* 也等價於
make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
*/
}];
}
設定view的size
make.size.mas_equalTo(CGSizeMake(40, 40));
更新刪除view的約束
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
/*
mas_makeConstraints 只負責新增約束 Autolayout不能同時存在兩條針對於同一物件的約束 否則會報錯
mas_updateConstraints 針對上面的情況 會更新在block中出現的約束 不會導致出現兩個相同約束的情況
mas_remakeConstraints 則會清除之前的所有約束 僅保留最新的約束
三種函式善加利用 就可以應對各種情況了
*/
mas_equalTo()和equalTo()的區別
mas_equalTo支援的型別更多
#define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))
#define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
#define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
#define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__)))
一個view
居中
///* 居中
make.size.mas_equalTo(CGSizeMake(100, 60));
make.centerY.equalTo(view.superview.mas_centerY);
make.centerX.equalTo(view.superview.mas_centerX);
// */
多個view
大小相同
make.width.equalTo(v1.mas_width);
make.height.equalTo(v1.mas_height);
首尾相連
//與v2上下間距為10
make.bottom.mas_equalTo(v2.mas_top).offset(-10);
//與v1左右間距為10
make.left.equalTo(v1.mas_right).offset(10);
同一垂直中線
//與v1的x中心點在同一直線
make.centerX.equalTo(v1.mas_centerX);
示例
- (void)towView{
UIView *v1 = [UIView new];
v1.backgroundColor = [UIColor redColor];
[self.view addSubview:v1];
UIView *v2 = [UIView new];
v2.backgroundColor = [UIColor blackColor];
[self.view addSubview:v2];
[v1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(200, 120));
make.top.mas_equalTo(v1.superview).offset(74);
//與v2上下間距為10
make.bottom.mas_equalTo(v2.mas_top).offset(-10);
make.centerX.equalTo(v1.superview.mas_centerX);
}];
[v2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(250, 30));
make.centerX.equalTo(v1.mas_centerX);
/*和v1的寬高相等
make.width.equalTo(v1.mas_width);
make.height.equalTo(v1.mas_height);
//*/
make.top.mas_equalTo(v1.mas_bottom).offset(10);
make.bottom.mas_equalTo(v2.superview).offset(-10);
}];
}
九宮格排序
- (void)squaredUp{
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i<14; i++) {
[array addObject:[NSString stringWithFormat:@"%d",i]];
}
__block UIView *lastV = nil;
int list = 4;
int gap = 10;
for (int i = 0; i<array.count; i++) {
UILabel *label = [UILabel new];
label.text = array[i];
[self.view addSubview:label];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor magentaColor];
[label sizeToFit];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
if (lastV) {
make.width.equalTo(lastV.mas_width);
}else
make.size.mas_equalTo(CGSizeMake((self.view.frame.size.width - gap*(list + 1))/list, 20));
if (i%list == 0) {
make.left.equalTo(label.superview).offset(gap);
}
else{
make.left.equalTo(lastV.mas_right).offset(gap);
}
if (i%list == (list -1)) {
make.right.equalTo(label.superview).offset(-gap);
}
int top = 64+(i/list+1)*gap+(i/list*label.frame.size.height);
make.top.equalTo(label.superview).offset(top);
lastV = label;
}];
}
}
相關文章
- Masonry 原始碼學習整理原始碼
- IOS基礎-Masonry 練習iOS
- iOS學習之Masonry第三方框架iOS框架
- Masonry進階
- Masonry原始碼解讀原始碼
- iOS框架·Masonry原始碼深度解析及學習啟示:設計模式與鏈式程式設計思想iOS框架原始碼設計模式程式設計
- Masonry 原始碼解讀(上)原始碼
- Masonry 原始碼解讀(下)原始碼
- Masonry自動佈局使用
- Masonry 關於ScrollView的使用View
- iOS自動佈局——Masonry詳解iOS
- Masonry佈局簡單朋友圈
- CSS Masonry Layouts【一】之 multi-columnsCSS
- iOS開發之原始碼解析 - MasonryiOS原始碼
- 讀autolayout開源框架masonry想MVC框架MVC
- iOS開發-Masonry約束寬高比iOS
- 看完 `Masonry` 原始碼後的幾點思考?原始碼
- iOS Masonry 一些日常使用方法iOS
- 原始碼閱讀:Masonry(一)——從使用入手原始碼
- 關於 Masonry 的一些思考(下)
- iOS 自動佈局框架 – Masonry 詳解iOS框架
- iOS自動佈局框架 - Masonry詳解iOS框架
- 學習學習再學習
- Masonry實現原理並沒有那麼可怕
- Masonry佈局控制元件,自動換行控制元件
- Masonry介紹與使用實踐:快速上手Autolayout
- 深度學習——學習目錄——學習中……深度學習
- iOS進階之masonry細緻入微_MASUtilities.hiOS
- vue3 瀑布流 vue-masonry使用方法Vue
- 深度學習(一)深度學習學習資料深度學習
- 深度學習學習框架深度學習框架
- 強化學習-學習筆記3 | 策略學習強化學習筆記
- 學習產品快報09 | “CSDN學習”:增加學習提醒,提示學習不忘記
- 【強化學習】強化學習/增強學習/再勵學習介紹強化學習
- 學習ThinkPHP,學習OneThinkPHP
- 前端學習之Bootstrap學習前端boot
- 學而習之,成就學習
- 前端週刊第62期:學習學習再學習前端