本帖最后由 Simpon 于 2016-10-19 13:35 编辑
接下来在分享3个小案例:
1. [初级] 让两个高度为150的view垂直居中且等宽且等间隔排列 间隔为10(自动计算其宽度)
- int padding1 = 10;
- [sv2 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.mas_equalTo(sv.mas_centerY);
- make.left.equalTo(sv.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(sv.mas_centerY);
- make.left.equalTo(sv2.mas_right).with.offset(padding1);
- make.right.equalTo(sv.mas_right).with.offset(-padding1);
- make.height.mas_equalTo(@150);
- make.width.equalTo(sv2);
- }];
复制代码
代码效果
这里我们在两个子view之间互相设置的约束 可以看到他们的宽度在约束下自动的被计算出来了
2. [中级] 在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);
- }];
复制代码
尾部效果:
从scrollView的scrollIndicator可以看出 scrollView的内部已如我们所想排列好了
这里的关键就在于container这个view起到了一个中间层的作用 能够自动的计算uiscrollView的contentSize
3. [高级] 横向或者纵向等间隙的排列一组view
很遗憾 autoLayout并没有直接提供等间隙排列的方法(Masonry的官方demo中也没有对应的案例) 但是参考案例3 我们可以通过一个小技巧来实现这个目的 为此我写了一个Category
- @implementation UIView(Masonry_LJC)
- - (void) distributeSpacingHorizontallyWith:(NSArray*)views
- {
- NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
-
- for ( int i = 0 ; i < views.count+1 ; ++i )
- {
- UIView *v = [UIView new];
- [spaces addObject:v];
- [self addSubview:v];
-
- [v mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.equalTo(v.mas_height);
- }];
- }
-
- UIView *v0 = spaces[0];
-
- __weak __typeof(&*self)ws = self;
-
- [v0 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(ws.mas_left);
- make.centerY.equalTo(((UIView*)views[0]).mas_centerY);
- }];
-
- UIView *lastSpace = v0;
- for ( int i = 0 ; i < views.count; ++i )
- {
- UIView *obj = views[i];
- UIView *space = spaces[i+1];
-
- [obj mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(lastSpace.mas_right);
- }];
-
- [space mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(obj.mas_right);
- make.centerY.equalTo(obj.mas_centerY);
- make.width.equalTo(v0);
- }];
-
- lastSpace = space;
- }
-
- [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(ws.mas_right);
- }];
-
- }
- - (void) distributeSpacingVerticallyWith:(NSArray*)views
- {
- NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
-
- for ( int i = 0 ; i < views.count+1 ; ++i )
- {
- UIView *v = [UIView new];
- [spaces addObject:v];
- [self addSubview:v];
-
- [v mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.equalTo(v.mas_height);
- }];
- }
-
-
- UIView *v0 = spaces[0];
-
- __weak __typeof(&*self)ws = self;
-
- [v0 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(ws.mas_top);
- make.centerX.equalTo(((UIView*)views[0]).mas_centerX);
- }];
-
- UIView *lastSpace = v0;
- for ( int i = 0 ; i < views.count; ++i )
- {
- UIView *obj = views[i];
- UIView *space = spaces[i+1];
-
- [obj mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(lastSpace.mas_bottom);
- }];
-
- [space mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(obj.mas_bottom);
- make.centerX.equalTo(obj.mas_centerX);
- make.height.equalTo(v0);
- }];
-
- lastSpace = space;
- }
-
- [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(ws.mas_bottom);
- }];
- }
- @end
复制代码
简单的来测试一下
- UIView *sv11 = [UIView new];
- UIView *sv12 = [UIView new];
- UIView *sv13 = [UIView new];
- UIView *sv21 = [UIView new];
- UIView *sv31 = [UIView new];
- sv11.backgroundColor = [UIColor redColor];
- sv12.backgroundColor = [UIColor redColor];
- sv13.backgroundColor = [UIColor redColor];
- sv21.backgroundColor = [UIColor redColor];
- sv31.backgroundColor = [UIColor redColor];
- [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]];
- [sv showPlaceHolderWithAllSubviews];
- [sv hidePlaceHolder];
复制代码
代码效果
perfect! 简洁明了的达到了我们所要的效果
这里所用的技巧就是 使用空白的占位view来填充我们目标view的旁边 这点通过图上的空白标注可以看出来
Masonry源码下载
精华推荐:
|
|