iOS開發masonry的一些使用簡介


從一開始的純代碼計算frame,雖然自認為計算frame 剛剛的,但是到后來還是開始xib的自動約束和手動約束與frame搭配使用,經歷這幾種方式,大概一年前開始普遍使用masonry來代碼約束之后也躍躍欲試的自己體驗了把,感覺還不錯,分享下,比原生的好使多了。

使用步驟

 1.添加Masonry文件夾的所有源代碼到項目中(共兩個Masonry這個文件夾,以及Masonry.framework)

 2.添加兩個宏定義導入頭文件

  // 只要添加了這個宏,就不用帶mas_前綴

  #define MAS_SHORTHAND

  // 只要添加了這個宏,equalTo就等價於mas_equalTo

 #define MAS_SHORTHAND_GLOBALS

  // 這個頭文件一定要放在上面兩個宏的后面

  #import "Masonry.h"

 

 下面是添加約束的方法

 // 這個方法只會添加新的約束

 2  [view makeConstraints:^(MASConstraintMaker *make) {

 3

 4  }];

 5

 6 // 這個方法會將以前的所有約束刪掉,添加新的約束

 7  [view remakeConstraints:^(MASConstraintMaker *make) {

 8

 9  }];

 10

 11  // 這個方法將會覆蓋以前的某些特定的約束

 12  [view updateConstraints:^(MASConstraintMaker *make) {

 13

 14  }];

 

 約束的類型

 1.尺寸:width\height\size

 2.邊界:left\leading\right\trailing\top\bottom

 3.中心點:center\centerX\centerY

 4.邊界:edges

*/

 

 

基本約束

UIView *superView = self.view;

    //三個view布滿整個屏幕

    UIView *leftView = [[UIView alloc] init];

    leftView.backgroundColor = [UIColor yellowColor];

    [superView addSubview:leftView];

    

    UIView *rightView = [[UIView alloc] init];

    rightView.backgroundColor = [UIColor grayColor];

    [superView addSubview:rightView];

    

    UIView *bottomView = [[UIView alloc] init];

    bottomView.backgroundColor = [UIColor greenColor];

    [superView addSubview:bottomView];

    

    int spacing = 0;

    

    [leftView makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view).offset(spacing);

        make.top.equalTo(superView).offset(64);

        make.right.equalTo(rightView.mas_leftMargin).offset(spacing);

        make.bottom.equalTo(superView.mas_centerY).offset(32);

    }];

    

    [rightView makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(leftView.mas_rightMargin).offset(spacing);

        make.right.equalTo(superView).offset(spacing);

        make.top.equalTo(superView).offset(64);

        make.width.equalTo(leftView);

        make.height.equalTo(leftView);

        

    }];

    

    [bottomView makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(superView).offset(0);

        make.right.equalTo(superView).offset(0);

        make.bottom.equalTo(superView).offset(0);

        make.top.equalTo(leftView.mas_bottom).offset(0);

        make.height.equalTo(leftView);

    }];

 

更換所有約束

- (void)viewDidLoad {

    [super viewDidLoad];

    

    yesOrNo = YES;

    

    thisBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [thisBtn setTitle:@"click" forState:UIControlStateNormal];

    [thisBtn addTarget:self action:@selector(updateMas) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:thisBtn];

    

    [thisBtn makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view).offset(10);

        make.top.equalTo(self.view).offset(64 + 10);

        make.width.equalTo(100);

        make.height.equalTo(30);

    }];

    

    // Do any additional setup after loading the view.

}

 

-(void)updateMas

{

    int num = arc4random()%300;

    NSLog(@"%d",num);

    [thisBtn remakeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(self.view).equalTo(- num - 20);

        make.left.equalTo(self.view).equalTo(num);

        make.width.equalTo(100);

        make.height.equalTo(30);

    }];

    

}

 

更新某個約束

- (void)viewDidLoad {

    [super viewDidLoad];

    

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"click" forState:UIControlStateNormal];

    btn.backgroundColor = [UIColor grayColor];

    [btn addTarget:self action:@selector(update) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    [btn makeConstraints:^(MASConstraintMaker *make) {

        //make.centerX.equalTo(self.view.centerX);

        //make.centerY.equalTo(self.view.centerY);

        make.center.equalTo(self.view);

        make.width.equalTo(100);

        make.height.equalTo(30);

    }];

    

    

    // Do any additional setup after loading the view.

}

 

-(void)update

{

    int width = arc4random()%300 +30;

    [btn updateConstraints:^(MASConstraintMaker *make) {

        make.width.equalTo(width);

    }];

}

 

Scrollview

 

#import "ScrollviewViewController.h"

 

@interface ScrollviewViewController ()

@property(nonatomic,strong)UIScrollView *myScrollview;

@end

 

@implementation ScrollviewViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    _myScrollview = [[UIScrollView alloc] init];

    _myScrollview.backgroundColor = [UIColor grayColor];

    [self.view addSubview:_myScrollview];

    [self.myScrollview makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.view);

    }];

    

    [self makeScr];

    // Do any additional setup after loading the view.

}

 

 

-(void)makeScr

{

    UIView *contentView = UIView.new;

    [self.myScrollview addSubview:contentView];

    

    [contentView makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.myScrollview);

        make.width.equalTo(self.myScrollview);

    }];

    

    UIView *lastView;

    CGFloat height = 35 + arc4random()%100;

    

    for (int i=0 ; i< 16; i++) {

        UIView *view = UIView.new;

        view.backgroundColor =[self randomColor];

        [contentView addSubview:view];

        [view makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(lastView ? lastView.bottom : @0);

            make.left.equalTo(@0 );

            make.width.equalTo(contentView.width);

            make.height.equalTo(@(height));

        }];

        

        lastView = view;

    }

    

    [contentView makeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(lastView.bottom);

    }];

    

}

- (UIColor *)randomColor {

    CGFloat hue = ( arc4random() % 255 / 255.0 );  //  0.0 to 1.0

    CGFloat saturation = ( arc4random() % 128 / 255.0 ) + 0.5;  //  0.5 to 1.0, away from white

    CGFloat brightness = ( arc4random() % 128 / 255.0 ) + 0.5;  //  0.5 to 1.0, away from black

    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];

}

 

 

以上是一些最基本的簡單使用,具體的還得自己加以研究啊。

需要注意的:

1 控件必需先添加在給約束。

 

以下是約束的代碼demo

 MasonryDemo.zip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM