自动布局Masonry更新约束

上一篇写了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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值