iOS键盘优化细节

来源:互联网 发布:知之深爱之切内容摘要 编辑:程序博客网 时间:2024/06/05 00:35


在延展中加入


/**输入工具条底部的约束*/

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputToolBarBottomConstraint;



修改  viewDidLoad方法如下


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //1.监听键盘弹出,把inputToolbar(输入工具条)往上移

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillShow:) name:UIKeyboardWillShowNotification object:nil];

    

    //2.监听键盘退出,inputToolbar恢复原位

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillHide:) name:UIKeyboardWillHideNotification object:nil];

}



在类中加入方法


#pragma mark 键盘显示时会触发的方法

-(void)kbWillShow:(NSNotification *)noti{

    

    //1.获取键盘高度

    //1.1获取键盘结束时候的位置

    CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat kbHeight = kbEndFrm.size.height;

    

    

    //2.更改inputToolbar底部约束

    self.inputToolBarBottomConstraint.constant = kbHeight;

    //添加动画

    [UIView animateWithDuration:0.25 animations:^{

        [self.view layoutIfNeeded];

    }];

    

    

}

#pragma mark 键盘退出时会触发的方法

-(void)kbWillHide:(NSNotification *)noti{

    //inputToolbar恢复原位

    self.inputToolBarBottomConstraint.constant =0;

}



-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}



0 0
原创粉丝点击