您的位置:首页 > 其它

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);
}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: