Masonry是一個輕量級的OC布局框架, 擁有自己的描述語法,采用更優(yōu)雅的鏈式語法封裝自動布局,簡潔明了,并具有高可讀性,而且同時支持 iOS 和 Max OS X。
Masonry支持的屬性與NSLayoutAttrubute的對照:
| MASConstraint (Masonry) | NSAutoLayout | 說明 |
|---|---|---|
| left | NSLayoutAttributeLeft | 左側 |
| top | NSLayoutAttributeTop | 上側 |
| right | NSLayoutAttributeRight | 右側 |
| bottom | NSLayoutAttributeBottom | 下側 |
| leading | NSLayoutAttributeLeading | 首部 |
| trailing | NSLayoutAttributeTrailing | 尾部 |
| width | NSLayoutAttributeWidth | 寬 |
| height | NSLayoutAttributeHeight | 高 |
| centerX | NSLayoutAttributeCenterX | 橫向中點 |
| centerY | NSLayoutAttributeCenterY | 縱向中點 |
| baseline | NSLayoutAttributeBaseline | 文本基線 |
其中l(wèi)eading與left, trailing與right 在正常情況下是等價的。但是當一些布局是從右至左時(比如阿拉伯文) 則會對調(diào), 換句話說就是基本可以 用left和right就好了。
- 在Masonry中能夠添加AutoLayout約束有三個函數(shù):
/*
mas_makeConstraints 只負責新增約束 Autolayout不能同時存在兩條針對于同一對象的約束 否則會報錯
mas_updateConstraints 針對上面的情況 會更新在block中出現(xiàn)的約束 不會導致出現(xiàn)兩個相同約束的情況
mas_remakeConstraints 則會清除之前的所有約束 僅保留最新的約束
*/
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
-
equalTo和mas_equalTo的區(qū)別?
mas_equalTo是宏,在MASConstraint.h文件中定義:
#define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))
mas_equalTo 比equalTo多了類型轉換操作, 大多數(shù)時候兩個方法都是 通用的,但是對于數(shù)值元素使用mas_equalTo。對于對象或是多個屬性的處理,使用equalTo。
以幾個??學習Masonry:
1 中心點與self.view相同,寬度為300*300
// exp1: 中心點與self.view相同,寬度為300*300
-(void)exp1 {
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
}
中心點與self.view相同,寬度為300*300
2 上下左右邊距都為10
-(void)exp2 {
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// 寫法1
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
/*
// 寫法2
make.top.equalTo(self.view).with.offset(10);
make.left.equalTo(self.view).with.offset(10);
make.bottom.equalTo(self.view).with.offset(-10);
make.right.equalTo(self.view).with.offset(-10);
*/
/*
// 寫法3
make.top.left.bottom.and.right.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
*/
}];
}
上下左右邊距都為10
- edges 就是top,left,bottom,right簡化寫法
- 寫法2中 bottom和right的offset是負數(shù),因為計算的是絕對的數(shù)值,也就是view的bottom的y值減去self.view的bottom的y值是-10,view的right的x值減去self.view的right的x值是-10。
-
and和with什么事件都沒做,只是從應用語法看上去很順。
3 讓兩個高度為150的view垂直居中且等寬且等間隔排列 間隔為10(自動計算器高度)
-(void)exp3{
UIView *view1 = [UIView new];
[view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view1];
UIView *view2 = [UIView new];
[view2 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view2];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view.mas_centerY);
make.height.mas_equalTo(150);
make.width.mas_equalTo(view2.mas_width);
make.left.mas_equalTo(self.view.mas_left).with.offset(10);
make.right.mas_equalTo(view2.mas_left).offset(-10);
}];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.view.mas_centerY);
make.height.mas_equalTo(150);
make.width.mas_equalTo(view1.mas_width);
make.left.mas_equalTo(view1.mas_right).with.offset(10);
make.right.equalTo(self.view.mas_right).offset(-10);
}];
}
讓兩個高度為150的view垂直居中且等寬且等間隔排列 間隔為10
4 iOS自帶計算器布局
-(void)exp4{
//申明區(qū)域,displayView是顯示區(qū)域,keyboardView是鍵盤區(qū)域
UIView *displayView = [UIView new];
[displayView setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:displayView];
UIView *keyboardView = [UIView new];
[self.view addSubview:keyboardView];
//先按1:3分割 displView(顯示結果區(qū)域)和 keyboardView(鍵盤區(qū)域)
[displayView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top);
make.left.and.right.equalTo(self.view);
make.height.equalTo(keyboardView).multipliedBy(0.3f);
}];
[keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(displayView.mas_bottom);
make.bottom.equalTo(self.view.mas_bottom);
make.left.and.right.equalTo(self.view);
}];
//設置顯示位置的數(shù)字為0
UILabel *displayNum = [[UILabel alloc]init];
[displayView addSubview:displayNum];
displayNum.text = @"0";
displayNum.font = [UIFont fontWithName:@"HeiTi SC" size:70];
displayNum.textColor = [UIColor whiteColor];
displayNum.textAlignment = NSTextAlignmentRight;
[displayNum mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(displayView).with.offset(-10);
make.bottom.equalTo(displayView).with.offset(-10);
}];
//定義鍵盤鍵名稱,?號代表合并的單元格
NSArray *keys = @[@"AC",@"+/-",@"%",@"÷"
,@"7",@"8",@"9",@"x"
,@"4",@"5",@"6",@"-"
,@"1",@"2",@"3",@"+"
,@"0",@"?",@".",@"="];
int indexOfKeys = 0;
for (NSString *key in keys){
//循環(huán)所有鍵
indexOfKeys++;
int rowNum = indexOfKeys %4 ==0? indexOfKeys/4:indexOfKeys/4 +1;
int colNum = indexOfKeys %4 ==0? 4 :indexOfKeys %4;
NSLog(@"index is:%d and row:%d,col:%d",indexOfKeys,rowNum,colNum);
//鍵樣式
UIButton *keyView = [UIButton buttonWithType:UIButtonTypeCustom];
[keyboardView addSubview:keyView];
[keyView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[keyView setTitle:key forState:UIControlStateNormal];
[keyView.layer setBorderWidth:1];
[keyView.layer setBorderColor:[[UIColor blackColor]CGColor]];
[keyView.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30]];
//鍵約束
[keyView mas_makeConstraints:^(MASConstraintMaker *make) {
//處理 0 合并單元格
if([key isEqualToString:@"0"] || [key isEqualToString:@"?"] ){
if([key isEqualToString:@"0"]){
[keyView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(keyboardView.mas_height).with.multipliedBy(.2f);
make.width.equalTo(keyboardView.mas_width).multipliedBy(.5);
make.left.equalTo(keyboardView.mas_left);
make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.9f);
}];
}if([key isEqualToString:@"?"]){
[keyView removeFromSuperview];
}
}
//正常的單元格
else{
make.width.equalTo(keyboardView.mas_width).with.multipliedBy(.25f);
make.height.equalTo(keyboardView.mas_height).with.multipliedBy(.2f);
//按照行和列添加約束,這里添加行約束
switch (rowNum) {
case 1:
{
make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.1f);
keyView.backgroundColor = [UIColor colorWithRed:205 green:205 blue:205 alpha:1];
}
break;
case 2:
{
make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.3f);
}
break;
case 3:
{
make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.5f);
}
break;
case 4:
{
make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.7f);
}
break;
case 5:
{
make.baseline.equalTo(keyboardView.mas_baseline).with.multipliedBy(.9f);
}
break;
default:
break;
}
//按照行和列添加約束,這里添加列約束
switch (colNum) {
case 1:
{
make.left.equalTo(keyboardView.mas_left);
}
break;
case 2:
{
make.right.equalTo(keyboardView.mas_centerX);
}
break;
case 3:
{
make.left.equalTo(keyboardView.mas_centerX);
}
break;
case 4:
{
make.right.equalTo(keyboardView.mas_right);
[keyView setBackgroundColor:[UIColor colorWithRed:243 green:127 blue:38 alpha:1]];
}
break;
default:
break;
}
}
}];
}
}
計算器
代碼: MasonryDemo