Masonry使用

  • (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
  • (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
  • (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
// 定義這個(gè)常量,就可以不用在開發(fā)過程中使用"mas_"前綴。
#define MAS_SHORTHAND
// 定義這個(gè)常量,就可以讓Masonry幫我們自動(dòng)把基礎(chǔ)數(shù)據(jù)類型的數(shù)據(jù),自動(dòng)裝箱為對(duì)象類型。
#define MAS_SHORTHAND_GLOBALS
//  #import "Masonry.h"  這個(gè)頭文件一定要放在上面兩個(gè)宏的后面
mas_makeConstraints 這個(gè)方法只會(huì)添加新的約束
mas_updateConstraints 這個(gè)方法將會(huì)覆蓋以前的某些特定的約束(更新約束)
mas_remakeConstraints 這個(gè)方法會(huì)將以前的所有約束刪掉,添加新的約束
例:
//viewDidLoad
    [_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        make.center.equalTo(strongSelf.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_view2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(150, 150));
    }];
}
//viewDidLoad
[_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        make.center.equalTo(strongSelf.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
 }];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_view2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(150, 150));
    }];
}
//由于mas_remakeConstraints清除之前的所有約束,所以view2的x,y沒有約束了,出現(xiàn)在(0,0)位置上
設(shè)置約束優(yōu)先級(jí)
    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    UIView *greenView = [[UIView alloc]init];
    greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:greenView];
    UIView *blueView = [[UIView alloc]init];
    blueView.backgroundColor  = [UIColor blueColor];
    [self.view addSubview:blueView];
//在使用Masonry添加約束之前,需要在addSubview之后才能使用,否則會(huì)導(dǎo)致崩潰。

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
    }];
    [greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(redView.mas_right).offset(20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
    }];
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(greenView.mas_right).offset(20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.2);
        make.left.equalTo(redView.mas_right).offset(20).priority(250);
      //make.left.equalTo(redView.mas_right).offset(20).priorityLow();
//Masonry為我們提供了三個(gè)默認(rèn)的方法,priorityLow()、priorityMedium()、priorityHigh(),這三個(gè)方法內(nèi)部對(duì)應(yīng)著不同的默認(rèn)優(yōu)先級(jí)。
 //除了這三個(gè)方法,我們也可以自己設(shè)置優(yōu)先級(jí)的值,可以通過priority()方法來(lái)設(shè)置。
//優(yōu)先級(jí)的范圍是0~1000,數(shù)字越大,優(yōu)先級(jí)越高,在不設(shè)置的情況下默認(rèn)為1000
//最后添加的這個(gè)約束的優(yōu)先級(jí)是低的,這個(gè)約束只有在它的沖突約束被抹掉后,它才能實(shí)現(xiàn)
    }];


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.greenView removeFromSuperview];
    [UIView animateWithDuration:1.0f animations:^{
        [self.view layoutIfNeeded];
    }];
}
大于等于和小于等于某個(gè)值的約束
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self.view);
    // 設(shè)置寬度小于等于200
    make.width.lessThanOrEqualTo(@200);
    // 設(shè)置高度大于等于10
    make.height.greaterThanOrEqualTo(@(10));
}];
設(shè)置約束比例
// 設(shè)置當(dāng)前約束值乘以多少,例如這個(gè)例子是redView的寬度是self.view寬度的0.2倍。
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self.view);
    make.height.mas_equalTo(30);
    make.width.equalTo(self.view).multipliedBy(0.2);
}];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載:https://www.cnblogs.com/liutingIOS/p/5406858.html 一、Ma...
    JasonYuan123閱讀 1,498評(píng)論 0 1
  • [置頂]iOS - Masonry使用中的一些整理 標(biāo)簽:iOS資源大全iOS常用方法iOS學(xué)習(xí)資料Masonry...
    DreamMakerSky閱讀 3,271評(píng)論 0 4
  • 個(gè)人Github博客,求關(guān)注 1 理解自身內(nèi)容尺寸約束與抗壓抗拉 自身內(nèi)容尺寸約束:一般來(lái)說(shuō),要確定一個(gè)視圖的精確...
    宿于松下閱讀 2,392評(píng)論 0 5
  • Masonry使用總結(jié) 一、Masonry簡(jiǎn)介 Masonry是一個(gè)輕量級(jí)的布局框架,適用于iOS以及OS X。它...
    BLSTUDIO閱讀 21,354評(píng)論 5 59
  • “如果哪天我要娶你,你會(huì)不會(huì)嫁我?”老徐如是對(duì)我說(shuō)。 半夜凌晨,老徐上夜班,我們?cè)谖⑿派衔遗闼e聊的時(shí)候,他忽然說(shuō)...
    驕傲的胡鬧閱讀 386評(píng)論 0 0

友情鏈接更多精彩內(nèi)容