Last active
March 3, 2017 11:42
-
-
Save Alvis-Li/9764f520b18637679c5d1e11ee932cec to your computer and use it in GitHub Desktop.
iOS 使用Masonry创建九宫格
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import "Masonry.h" | |
@interface ViewController (){ | |
UIView *bView; | |
} | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
bView = [UIView new]; | |
bView.backgroundColor = [UIColor lightGrayColor]; | |
[self.view addSubview:bView]; | |
CGFloat width = [UIScreen mainScreen].bounds.size.width - 30; | |
[bView mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.left.mas_equalTo(15); | |
make.right.mas_equalTo(-15); | |
make.centerY.mas_equalTo(self.view); | |
make.height.equalTo(@(width*0.618)); | |
}]; | |
NSArray *views = @[@[@1,@2,@3]]; | |
[self setSubViewLayout:bView views:views count:2]; | |
} | |
-(void)setSubViewLayout:(UIView *)supView views:(NSArray *)views count:(NSInteger)count { | |
if (views.count == 1) { | |
UIView *singleSupView = [self singleLayout:supView tag:10000 class:@"UIView" info:nil]; | |
NSArray *tmpArr = [views firstObject]; | |
if (singleSupView && tmpArr.count == 1) { | |
[self singleLayout:singleSupView tag:10000 class:@"UIImageView" info:nil]; | |
} else if(singleSupView && tmpArr.count > 1){ | |
[self multipleLayout:singleSupView supTag:10000 class:@"UIImageView" arr:tmpArr axisType:MASAxisTypeHorizontal]; | |
CGFloat width = [UIScreen mainScreen].bounds.size.width - 60 - 5*(tmpArr.count -1); | |
[bView mas_updateConstraints:^(MASConstraintMaker *make) { | |
make.height.equalTo(@(width*1.0/tmpArr.count + 9)); | |
}]; | |
} | |
}else if(views.count > 1){ | |
CGFloat width = [UIScreen mainScreen].bounds.size.width - 60; | |
if (count == 4) { | |
[bView mas_updateConstraints:^(MASConstraintMaker *make) { | |
make.height.equalTo(@(width*1.0 + 15)); | |
}]; | |
} else if(count == 6){ | |
CGFloat width = [UIScreen mainScreen].bounds.size.width - 60 - 10; | |
[bView mas_updateConstraints:^(MASConstraintMaker *make) { | |
make.height.equalTo(@(width*1.0/3.0*2.0 + 17)); | |
}]; | |
}else if(count >= 7 && count <= 9){ | |
CGFloat width = [UIScreen mainScreen].bounds.size.width - 60 - 10; | |
[bView mas_updateConstraints:^(MASConstraintMaker *make) { | |
make.height.equalTo(@(width*1.0+25)); | |
}]; | |
} | |
NSArray * tmpViews = [self multipleLayout:supView supTag:10000 class:@"UIView" arr:views axisType:MASAxisTypeVertical]; | |
for (int m = 0; m < tmpViews.count ; m++) { | |
NSArray * tmpArr = views[m]; | |
UIView *tmpView = tmpViews[m]; | |
if (tmpView && tmpArr.count == 1) { | |
[self singleLayout:tmpView tag:tmpView.tag class:@"UIImageView" info:@{@"url":@"url"}]; | |
} else if(tmpView && tmpArr.count > 1){ | |
[self multipleLayout:tmpView supTag:tmpView.tag class:@"UIImageView" arr:tmpArr axisType:MASAxisTypeHorizontal]; | |
} | |
} | |
} | |
} | |
-(UIView *)singleLayout:(UIView *)supView tag:(NSInteger)tag class:(NSString *)class info:(NSDictionary *)info{ | |
UIView *single = [NSClassFromString(class) new]; | |
if ([class isEqual: @"UIView"]) { | |
single.tag = tag + 100; | |
}else if([class isEqual: @"UIImageView"]){ | |
single.tag = tag + 1; | |
} | |
single.backgroundColor = supView.backgroundColor; | |
[supView addSubview:single]; | |
[single mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.edges.mas_equalTo(supView); | |
}]; | |
return single; | |
} | |
-(NSArray *)multipleLayout:(UIView *)supView supTag:(NSInteger)supTag class:(NSString *)class arr:(NSArray *)arr axisType:(MASAxisType)axisType { | |
NSInteger spacing = 5; | |
NSMutableArray *tmpViews = [@[] mutableCopy]; | |
if ([class isEqual: @"UIView"]) { | |
spacing = 0; | |
} | |
for (int m = 0; m< arr.count; m++) { | |
UIView * bottomView = [NSClassFromString(class) new]; | |
bottomView.backgroundColor = [UIColor whiteColor]; | |
if ([class isEqual: @"UIView"]) { | |
bottomView.tag = supTag + 100*m; | |
}else if([class isEqual: @"UIImageView"]){ | |
bottomView.tag = supTag + 1*m; | |
} | |
[supView addSubview:bottomView]; | |
[tmpViews addObject:bottomView]; | |
} | |
[supView.subviews mas_distributeViewsAlongAxis:axisType withFixedSpacing:spacing leadSpacing:spacing*2 tailSpacing:spacing*2]; | |
if (axisType == MASAxisTypeHorizontal) { | |
[supView.subviews mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.top.equalTo(@(0)); | |
make.height.mas_equalTo(supView).offset(-(spacing)); | |
}]; | |
}else if(axisType == MASAxisTypeVertical){ | |
[supView.subviews mas_makeConstraints:^(MASConstraintMaker *make) { | |
make.left.equalTo(@(spacing*0.5)); | |
make.width.mas_equalTo(supView).offset(-(spacing*2)); | |
}]; | |
} | |
return tmpViews; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment