自动布局Masonry更新约束
来源:互联网 发布:小米笔记本装linux 编辑:程序博客网 时间:2024/05/20 15:39
上一篇写了Masonry设置约束的基本使用,而实际开发中我们有时候不仅要加约束,还需要更新约束,所以本篇就专门来写下Masonry是如何更新约束的,其实很简单,Masonry中可以使用mas_updateConstraints来更新约束,也可以使用mas_remakeConstraints,两者的区别是update是更新约束,而remake会在添加约束前去掉之前的约束。大家可以根据自己的实际需求使用,我这里的例子用的是update来更新约束。
在一些APP中我们经常可以看到,尤其是登录界面,当键盘弹起时会遮挡到部分文本框,这个时候往往是将文本框的位置上移。本文中的小demo就是类似是这种功能,点击绿色的文本框,键盘弹出时让文本框移到键盘之上,结束编辑后又回到原位。
先看下效果图吧:
首先,让我们来添加子控件:
/** 初始化子控件 */-(void)setupSubviews{ //文本框textview UITextField *greenTextView=[[UITextField alloc]init]; greenTextView.backgroundColor = [UIColor greenColor]; greenTextView.placeholder = @"点我更新约束啦..."; self.greenTextView = greenTextView; [self.view addSubview:greenTextView]; //设置初始时的约束 //左边距离self.view左边10 //底部距离self.view 20 //高度为50 //宽度自适应 [greenTextView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.view.mas_leading).offset(10); make.bottom.equalTo(self.view.mas_bottom).offset(-20); make.height.mas_equalTo(50); }]; //按钮 UIButton *blueBtn = [[UIButton alloc]init]; blueBtn.backgroundColor = [UIColor blueColor]; self.blueBtn = blueBtn; [self.view addSubview:blueBtn]; //设置初始时的约束 //右边距离self.view右边10 //左边距离self.greenTextView右边 10 //宽高和self.greenTextView一样 [blueBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.trailing.equalTo(self.view.mas_trailing).offset(-10); make.leading.equalTo(self.greenTextView.mas_trailing).offset(10); make.bottom.equalTo(self.greenTextView.mas_bottom); make.size.equalTo(self.greenTextView); }];}
接下来就是在监听键盘弹出的方法里更新约束了,注释都写在代码里了。
//键盘弹出时会调用-(void)keyboardWillShow:(NSNotification *)notification{ //获取键盘的基本信息 NSDictionary *userInfo = [notification userInfo]; CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue]; CGFloat keyboardHeight = CGRectGetHeight(rect); CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //修改下边距约束 [self.greenTextView mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-keyboardHeight-20); }]; //更新约束 [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }];}
这样就实现了当点击文本框编辑时,文本框上移的功能了。收回键盘的代码跟弹出的一样,这里不单独拿出来了,文章最后会贴上完整的代码。
注意添加监听以及导入Masonry哦,否则导入头文件是会报错的,是不是很简单!
好了,需要完整代码的童鞋看这里吧:
#import "ViewController.h"#import "Masonry.h"@interface ViewController ()@property(nonatomic,strong)UITextField *greenTextView;@property(nonatomic,strong)UIButton *blueBtn;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //初始化子控件 [self setupSubviews]; //键盘弹出监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //键盘收回监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];}/** 初始化子控件 */-(void)setupSubviews{ //文本框textview UITextField *greenTextView=[[UITextField alloc]init]; greenTextView.backgroundColor = [UIColor greenColor]; greenTextView.placeholder = @"点我更新约束啦..."; self.greenTextView = greenTextView; [self.view addSubview:greenTextView]; //设置初始时的约束 //左边距离self.view左边10 //底部距离self.view 20 //高度为50 //宽度自适应 [greenTextView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.view.mas_leading).offset(10); make.bottom.equalTo(self.view.mas_bottom).offset(-20); make.height.mas_equalTo(50); }]; //按钮 UIButton *blueBtn = [[UIButton alloc]init]; blueBtn.backgroundColor = [UIColor blueColor]; self.blueBtn = blueBtn; [self.view addSubview:blueBtn]; //设置初始时的约束 //右边距离self.view右边10 //左边距离self.greenTextView右边 10 //宽高和self.greenTextView一样 [blueBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.trailing.equalTo(self.view.mas_trailing).offset(-10); make.leading.equalTo(self.greenTextView.mas_trailing).offset(10); make.bottom.equalTo(self.greenTextView.mas_bottom); make.size.equalTo(self.greenTextView); }];}//键盘弹出时会调用-(void)keyboardWillShow:(NSNotification *)notification{ //获取键盘的基本信息 NSDictionary *userInfo = [notification userInfo]; CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue]; CGFloat keyboardHeight = CGRectGetHeight(rect); CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //修改下边距约束 [self.greenTextView mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-keyboardHeight-20); }]; //更新约束 [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }];}//键盘收回时会调用-(void)keyboardWillHidden:(NSNotification *)notification{ NSDictionary *userInfo = [notification userInfo]; CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //修改下边距约束 [self.greenTextView mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-20); }]; //更新约束 [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }];}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //结束编辑 [self.view endEditing:YES];}@end
1 0
- 自动布局Masonry更新约束
- Masonry自动布局详解二:动画更新约束
- Masonry自动布局详解四:整体动画更新约束
- Masonry自动布局详解三:remake约束
- aotuLayout(masonry、snapKit)约束布局动画更新约束
- masonry 约束布局
- iOS - 使用"Masonry"库做约束(自动布局)
- Masonry学习之更新约束
- Masonry自动布局
- IOS Masonry自动布局
- Masonry 自动布局 cocopods
- 自动布局Masonry
- iOS 自动布局 Masonry
- masonry 轻量级自动布局
- Masonry自动布局
- Masonry - 自动布局
- 自动布局之masonry
- 自动布局之Masonry
- CSS3 transform介绍|如何设计炫酷的动画效果
- 动态规划之01背包问题
- git push error report: ! [remote rejected] master -> refs/for/master (no new changes)
- Python Tutorial 学习笔记6 --Errors and Exceptions
- Velocity #parse与#include区别以及权限
- 自动布局Masonry更新约束
- TCP选项之SO_LINGER
- 余额重估(Revaluation Balances)相关设置和操作
- HTTPS为什么安全 &分析 HTTPS 连接建立全过程
- 软件开发模式对比(瀑布、迭代、螺旋、敏捷)
- C++内存管理
- RPM一些常用的命令
- Java io操作,poi导出excel,集合自带排序,日志报告
- redis存储和查询的java实现