ios - masonry第三方庫使用自動佈局(參考:http://www.cocoachina.com/ios/20141219/10702.html)

#import "ViewController.h"
#import "Masonry.h"
#define kWeakSelf(weakSelf) __weak typeof(self)weakSelf = self
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self
@interface ViewController ()
{
    UIView *topView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    kWeakSelf(ws);
    /**底部灰色view*/
    topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor lightGrayColor];
    [ws.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(ws.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
        
          }];
    /**表面紅色view*/
    UIView *redView= [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [topView addSubview:redView];
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(topView).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
        
    }];
    
    /**
     建立三個等寬等高等間距view
     */
    /**左邊view*/
    UIView *leftView = [[UIView alloc]init];
    leftView.backgroundColor = [UIColor blueColor];
    [redView addSubview:leftView];
    /**右邊view*/
    UIView *rightView = [[UIView alloc]init];
    rightView.backgroundColor = [UIColor brownColor];
    [redView addSubview:rightView];
    
    /**最有邊view*/
    UIView *lastView = [[UIView alloc]init];
    lastView.backgroundColor = [UIColor yellowColor];
    [redView addSubview:lastView];
    
    CGFloat padding = 10.f;
    //左邊view添加約束
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(redView.mas_centerY);
        make.left.equalTo(redView.mas_left).with.offset(padding);
        make.right.equalTo(rightView.mas_left).with.offset(-padding);
        make.height.mas_equalTo(@150);
        make.width.equalTo(rightView);
    }];
     //右邊view添加約束
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(redView.mas_centerY);
        make.left.equalTo(leftView.mas_right).with.offset(padding);
        make.right.equalTo(lastView.mas_left).with.offset(-padding);
        
        make.height.mas_equalTo(@150);
        make.width.equalTo(lastView);
        
    }];
     //最右邊view添加約束
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(redView.mas_centerY);
        make.left.equalTo(rightView.mas_right).with.offset(padding);
        make.right.equalTo(redView.mas_right).with.offset(-padding);
        make.width.equalTo(rightView);
        make.height.mas_equalTo(@150);
    }];
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end

相關文章
相關標籤/搜索