iOS

通知监听键盘的出现和隐藏,让工具条紧贴着键盘

2016-09-13  本文已影响0人  怪兽密保

http://www.cnblogs.com/zhanghuanan/p/5611675.html

//注册一个通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

//通知方法

-(void)keyboardWillChangeFrame:(NSNotification*)note

{

// 、显示或隐藏键盘时的高度

CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

//修改底部约束(需要改变的控件位置)

self.bottomSapce.constant=UIHight-frame.origin.y;

//动画时间

CGFloat duration=[note.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];

[UIView animateWithDuration:duration animations:^{

[self.view layoutIfNeeded];//改变控制的位置

}];

}

-(void)dealloc

{//取消通知。控制器消失时需要移除通知

[[NSNotificationCenter defaultCenter]removeObserver:self];

}

效果如下图:原来在底部的输入工具条,当键盘弹出时,上移一定高度,让工具条紧贴着键盘。

上一篇下一篇

猜你喜欢

热点阅读