Masonry使用详解
2015-11-30 17:03
281 查看
mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错
mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束
equalTo 和 mas_equalTo的区别在哪里呢? 其实 mas_equalTo是一个MACRO,比较的是值,equalTo比较的是view。
宽高相等:
make.width.offset(marginHeight);
make.height.mas_equalTo(timeImg.mas_width).multipliedBy(1);
1. [基础] 居中显示一个view
使用masonary就行九宫格布局:
-(void)viewDidLoad{
_topScrollButtonTitleArr=[[NSMutableArray alloc] initWithObjects:@"推荐", @"青春", @"淑女", @"女鞋", @"配饰", @"女包", @"泳装", @"内衣", @"婚礼", @"大码", @"老公" ,@"妈妈", @"爸爸", @"孕妇", @"男孩", @"女孩", @"生活", nil];
__block MainChoseLabel *nextLabel=nil;
for (int i=0;i<_topScrollButtonTitleArr.count;i++) {
MainChoseLabel *mainLabel=[MainChoseLabel new];
[self.view addSubview:mainLabel];
mainLabel.backgroundColor=[UIColor redColor];
mainLabel.text=_topScrollButtonTitleArr[i];
int col=i%4;
[mainLabel mas_updateConstraints:^(MASConstraintMaker *make) {
if(nextLabel){
if(col==0){
make.left.offset(10);
}else{
make.left.equalTo(nextLabel.mas_right).offset(10);
}
make.width.equalTo(nextLabel);
if(((i+1)%4)==0){
make.right.offset(-10);
NSLog(@"---");
}
}else{
make.left.offset(10);
}
make.height.offset(44);
make.top.offset((10+44)*(i/4)+30);
}];
nextLabel=mainLabel;
}
}
mas scrollview布局
_bigScrollView=[UIScrollView new];
_bigScrollView.pagingEnabled=YES;
_bigScrollView.delegate=self;
[self.view addSubview:_bigScrollView];
[_bigScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(120,5,5,5));
make.top.top.offset(0);
make.bottom.offset(0);
make.left.offset(0);
make.right.offset(0);
}];
_bigScrollView.backgroundColor=[UIColor redColor];
UIView *container = [UIView new];
[_bigScrollView addSubview:container];
container.backgroundColor=[UIColor redColor];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_bigScrollView);
make.height.equalTo(_bigScrollView);
}];
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.top.and.bottom.equalTo(container);
make.width.mas_equalTo(_bigScrollView);
if (lastView)
{
make.left.mas_equalTo(lastView.mas_right);
}
else
{
make.left.mas_equalTo(container.mas_left);
}
}];
lastView = subv;
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(lastView.mas_right);
}];
mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束
equalTo 和 mas_equalTo的区别在哪里呢? 其实 mas_equalTo是一个MACRO,比较的是值,equalTo比较的是view。
宽高相等:
make.width.offset(marginHeight);
make.height.mas_equalTo(timeImg.mas_width).multipliedBy(1);
1. [基础] 居中显示一个view
使用masonary就行九宫格布局:
-(void)viewDidLoad{
_topScrollButtonTitleArr=[[NSMutableArray alloc] initWithObjects:@"推荐", @"青春", @"淑女", @"女鞋", @"配饰", @"女包", @"泳装", @"内衣", @"婚礼", @"大码", @"老公" ,@"妈妈", @"爸爸", @"孕妇", @"男孩", @"女孩", @"生活", nil];
__block MainChoseLabel *nextLabel=nil;
for (int i=0;i<_topScrollButtonTitleArr.count;i++) {
MainChoseLabel *mainLabel=[MainChoseLabel new];
[self.view addSubview:mainLabel];
mainLabel.backgroundColor=[UIColor redColor];
mainLabel.text=_topScrollButtonTitleArr[i];
int col=i%4;
[mainLabel mas_updateConstraints:^(MASConstraintMaker *make) {
if(nextLabel){
if(col==0){
make.left.offset(10);
}else{
make.left.equalTo(nextLabel.mas_right).offset(10);
}
make.width.equalTo(nextLabel);
if(((i+1)%4)==0){
make.right.offset(-10);
NSLog(@"---");
}
}else{
make.left.offset(10);
}
make.height.offset(44);
make.top.offset((10+44)*(i/4)+30);
}];
nextLabel=mainLabel;
}
}
mas scrollview布局
_bigScrollView=[UIScrollView new];
_bigScrollView.pagingEnabled=YES;
_bigScrollView.delegate=self;
[self.view addSubview:_bigScrollView];
[_bigScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(120,5,5,5));
make.top.top.offset(0);
make.bottom.offset(0);
make.left.offset(0);
make.right.offset(0);
}];
_bigScrollView.backgroundColor=[UIColor redColor];
UIView *container = [UIView new];
[_bigScrollView addSubview:container];
container.backgroundColor=[UIColor redColor];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_bigScrollView);
make.height.equalTo(_bigScrollView);
}];
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.top.and.bottom.equalTo(container);
make.width.mas_equalTo(_bigScrollView);
if (lastView)
{
make.left.mas_equalTo(lastView.mas_right);
}
else
{
make.left.mas_equalTo(container.mas_left);
}
}];
lastView = subv;
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(lastView.mas_right);
}];
相关文章推荐
- CheckBox全选的实现
- Araxis Merge基本配置及操作
- 第十四周项目--是否二叉排序树
- 第十二周项目4 输出从顶点u到v的一条简单路径
- 第7周项目6-停车场模拟
- 第14周 项目1(4)-平衡二叉树
- Codeforces 599C Day at the Beach(想法题,排序)
- NSOperation自定义
- block全面分析
- FLASH芯片型号
- java-map按key升序排序
- mysql的auto-rehash简介
- Hbase Java客户端示例
- mysql的auto-rehash简介
- 写给笨人的法线贴图原理 【转】
- ajax跨域问题解决之cors篇
- 搭建ftp
- block全面分析
- 第13周项目1Floyd算法的验证
- 【第13周 项目4 - Floyd算法的验证】