masonry 基本用法
2017-06-19 10:52 甘超波 阅读(863) 评论(0) 编辑 收藏 举报一:masonry 基本用法
fistView=[[UIView alloc] init]; fistView.backgroundColor=[UIColor redColor]; [self.view addSubview:fistView]; secondView=[[UIView alloc] init]; secondView.backgroundColor=[UIColor blueColor]; [self.view addSubview:secondView]; threeView=[[UIView alloc] init]; threeView.backgroundColor=[UIColor yellowColor]; [self.view addSubview:threeView]; bottomView=[[UIView alloc] init]; bottomView.backgroundColor=[UIColor grayColor]; [self.view addSubview:bottomView];
基本约束布局代码
#pragma mark -第一种布局方法 -(void)left_top_size_marign{ CGFloat padding =10; CGFloat width=(self.view.bounds.size.width-4*padding)/3; [fistView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding); make.top.mas_equalTo(padding); make.size.mas_equalTo(CGSizeMake(width, 100)); }]; [threeView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding*2+width); make.top.mas_equalTo(padding); make.size.mas_equalTo(CGSizeMake(width, 100)); }]; [threeView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding*3+width*2); make.top.mas_equalTo(padding); make.size.mas_equalTo(CGSizeMake(width, 100)); }]; }
二:masonry 相对于子View布局
CGFloat padding =10; CGFloat width=(self.view.bounds.size.width-4*padding)/3; [fistView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding); make.top.mas_equalTo(padding); make.size.mas_equalTo(CGSizeMake(width, 100)); }]; [secondView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(fistView.mas_right).offset(padding); make.size.top.mas_equalTo(fistView); }]; [threeView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(secondView.mas_right).offset(padding); make.size.top.mas_equalTo(secondView); }];
三:masonry内边距布局
//内边距 [paddingView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(fistView).insets(UIEdgeInsetsMake(5, 5, 5, 5)); }];
四:UILable 多行布局
lb=[[UILabel alloc] init]; lb.text=@"ication:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add remote-notification to the list of your supported"; [self.view addSubview:lb]; --------- //label多行 lb.preferredMaxLayoutWidth=self.view.width-20; lb.numberOfLines=0; [lb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(bottomView.mas_bottom).offset(5); make.left.mas_equalTo(10); make.right.mas_equalTo(-10); }];
五:masonry动画更新
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ if(flag){ CGFloat padding =10; CGFloat width=(self.view.bounds.size.width-4*padding)/3; [fistView mas_updateConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding); make.top.mas_equalTo(padding); make.size.mas_equalTo(CGSizeMake(width, 100)); }]; } else{ [fistView mas_updateConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(5); make.top.mas_equalTo(5); make.size.mas_equalTo(CGSizeMake(200, 200)); }]; } flag=!flag; [UIView animateWithDuration:0.25 animations:^{ // [self.view layoutIfNeeded]; } completion:^(BOOL finished) { }]; }
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频