Masonry的一些等间距布局
- 控件之间的间距相等,但是控件的宽度是不定的。
下列的代码:定义间距为10,yellowview的宽度是由redView的宽度计算出来的。
UIView *redView = [[UIView alloc]init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(ws.view); make.top.mas_equalTo(ws.view); make.right.mas_equalTo(ws.view); }]; NSInteger padding = 10; UIView *yellowView1 = [[UIView alloc] init]; yellowView1.backgroundColor = [UIColor yellowColor]; [redView addSubview:yellowView1]; UIView *yellowView2 = [[UIView alloc] init]; yellowView2.backgroundColor = [UIColor yellowColor]; [redView addSubview:yellowView2]; UIView *yellowView3 = [[UIView alloc] init]; yellowView3.backgroundColor = [UIColor yellowColor]; [redView addSubview:yellowView3]; [@[yellowView1,yellowView2,yellowView3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:padding leadSpacing:padding tailSpacing:padding]; [@[yellowView1,yellowView2,yellowView3] mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(redView).offset(padding); make.height.mas_equalTo(yellowView1.mas_width); make.bottom.mas_equalTo(redView).offset(-padding); }];
- 控件的宽度是一定的,但是控件之间的间距是不定的。
下列的代码:定义控件的宽度为22,控件之间的间距是由redView的宽度计算出来的。
UIView *redView = [[UIView alloc]init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(ws.view); make.top.mas_equalTo(ws.view); make.right.mas_equalTo(ws.view); }]; UIView *yellowView1 = [[UIView alloc] init]; yellowView1.backgroundColor = [UIColor yellowColor]; [redView addSubview:yellowView1]; UIView *yellowView2 = [[UIView alloc] init]; yellowView2.backgroundColor = [UIColor yellowColor]; [redView addSubview:yellowView2]; UIView *yellowView3 = [[UIView alloc] init]; yellowView3.backgroundColor = [UIColor yellowColor]; [redView addSubview:yellowView3]; //控件的宽度 CGFloat viewW = 22; CGFloat padding = (SCREENWIDTH - 3*viewW) * 1.0 / 4; [@[yellowView1,yellowView2,yellowView3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:viewW leadSpacing:padding tailSpacing:padding]; [@[yellowView1,yellowView2,yellowView3] mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(redView).offset(padding); make.size.mas_equalTo(viewW); make.bottom.mas_equalTo(redView).offset(-padding); }];
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步