Masonry是一個輕量級的佈局框架 擁有自己的描述語法 採用更優雅的鏈式語法封裝自動佈局 簡潔明瞭 並具有高可讀性 而且同時支持 iOS 和 Max OS X。
Masonry是一個非常優秀的autolayout庫 能夠節省大量的開發和學習時間
Masonry支持的一些屬性
// 左側
@property (nonatomic, strong, readonly) MASConstraint *left;
// 頂部
@property (nonatomic, strong, readonly) MASConstraint *top;
// 右側
@property (nonatomic, strong, readonly) MASConstraint *right;
// 底部
@property (nonatomic, strong, readonly) MASConstraint *bottom;
// 首部
@property (nonatomic, strong, readonly) MASConstraint *leading;
// 尾部
@property (nonatomic, strong, readonly) MASConstraint *trailing;
// 寬
@property (nonatomic, strong, readonly) MASConstraint *width;
// 高
@property (nonatomic, strong, readonly) MASConstraint *height;
// 中心點x
@property (nonatomic, strong, readonly) MASConstraint *centerX;
// 中心點y
@property (nonatomic, strong, readonly) MASConstraint *centerY;
// 文本基線
@property (nonatomic, strong, readonly) MASConstraint *baseline;
這些屬性與NSLayoutAttrubute的對照表如下
下面是一個Masory工程的Demo
//
// ViewController.m
// Masnory_Demo
//
// Created by lanou3g on 15/10/29.
// Copyright © 2015年 . All rights reserved.
//
#import "ViewController.h"
#import "Masonry.h"
#import "UIView+masnory.h"
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
WS(ws);
//從此以後基本可以拋棄CGRectMake了
UIView *sv = [UIView new];
sv.backgroundColor = [UIColor blackColor];
//在做autoLayout之前 一定要先將view添加到superview上 否則會報錯
[self.view addSubview:sv];
//mas_makeConstraints就是Masonry的autolayout添加函數 將所需的約束添加到block中行了
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
//將sv居中
make.center.equalTo(ws.view);
//將size設置成(300,300)
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
#pragma mark -- [初級] 讓一個view略小於其superView(邊距爲10)
//
// UIView *sv1 = [UIView new];
// sv1.backgroundColor = [UIColor redColor];
// [sv addSubview:sv1];
// //距離sv這個視圖的邊距
// [sv1 mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(sv).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));
// */
// }];
#pragma mark -- [初級] 讓兩個高度爲150的view垂直居中且等寬且等間隔排列 間隔爲10(自動計算其寬度)
// UIView *sv2 = [UIView new];
// UIView *sv3 = [UIView new];
// sv2.backgroundColor = [UIColor blueColor];
// sv3.backgroundColor = [UIColor yellowColor];
//
// [sv1 addSubview:sv2];
// [sv1 addSubview:sv3];
//
// int padding1 = 10;
// [sv2 mas_makeConstraints:^(MASConstraintMaker *make) {
// make.centerY.mas_equalTo(sv1.mas_centerY);
// make.left.equalTo(sv1.mas_left).with.offset(padding1);
// make.right.equalTo(sv3.mas_left).with.offset(-padding1);
// make.height.mas_equalTo(@150);
// make.width.equalTo(sv3);
// }];
//
// [sv3 mas_makeConstraints:^(MASConstraintMaker *make) {
// make.centerY.mas_equalTo(sv1.mas_centerY);
// make.left.equalTo(sv2.mas_right).with.offset(padding1);
// make.right.equalTo(sv1.mas_right).with.offset(-padding1);
// make.height.mas_equalTo(@150);
// // make.width.equalTo(sv2); 上面已經寫過與其相等
// }];
#pragma mark -- [中級] 在UIScrollView順序排列一些view並自動計算contentSize
UIScrollView *scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor whiteColor];
[sv addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(5,5,5,5));
}];
UIView *container = [UIView new];
[scrollView addSubview:container];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.width.equalTo(scrollView);
}];
int count = 10;
UIView *lastView = nil;
for ( int i = 1 ; i <= count ; ++i )
{
UIView *subv = [UIView new];
[container addSubview:subv];
subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )
saturation:( arc4random() % 128 / 256.0 ) + 0.5
brightness:( arc4random() % 128 / 256.0 ) + 0.5
alpha:1];
[subv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(container);
make.height.mas_equalTo(@(20*i));
if ( lastView )
{
make.top.mas_equalTo(lastView.mas_bottom);
}
else
{
make.top.mas_equalTo(container.mas_top);
}
}];
lastView = subv;
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
#pragma mark -- [高級] 橫向或者縱向等間隙的排列一組view
UIView *sv11 = [UIView new];
UIView *sv12 = [UIView new];
UIView *sv13 = [UIView new];
UIView *sv21 = [UIView new];
UIView *sv31 = [UIView new];
sv11.backgroundColor = [UIColor blackColor];
sv12.backgroundColor = [UIColor blackColor];
sv13.backgroundColor = [UIColor blackColor];
sv21.backgroundColor = [UIColor blackColor];
sv31.backgroundColor = [UIColor blackColor];
[sv addSubview:sv11];
[sv addSubview:sv12];
[sv addSubview:sv13];
[sv addSubview:sv21];
[sv addSubview:sv31];
//給予不同的大小 測試效果
[sv11 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(@[sv12,sv13]);
make.centerX.equalTo(@[sv21,sv31]);
make.size.mas_equalTo(CGSizeMake(40, 40));
}];
[sv12 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(70, 20));
}];
[sv13 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 50));
}];
[sv21 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 20));
}];
[sv31 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(40, 60));
}];
[sv distributeSpacingHorizontallyWith:@[sv11,sv12,sv13]];
[sv distributeSpacingVerticallyWith:@[sv11,sv21,sv31]];
}
@end