iOS中在tableview上通知控制键盘弹起界面上推的方法
2016-01-19 14:37
495 查看
上代码:
//注册通知
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(openKeyboard:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(closeKeyboard:)
name:UIKeyboardWillHideNotification
object:nil];
}
//注销通知
-(void)viewDidDisAppear:(BOOL)animated{
[super viewDidDisappear:anima
4000
ted];
[[NSNotificationCenter
defaultCenter]removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter]removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
//键盘上弹
-(void)openKeyboard:(NSNotification *)notification{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
UIViewAnimationOptions option = [notification.userInfo [UIKeyboardAnimationCurveUserInfoKey]intValue];
// self.BottomLayoutConstaint.constant
这个是storyboard里面tableview的下沿,直接拉线的,而keyboardFrame.size.height是键盘的高度
self.BottomLayoutConstaint.constant = keyboardFrame.size.height+99*SizeScaleY;
[UIView
animateWithDuration:duration delay:0
options:option animations:^{
[self.view
layoutIfNeeded];
} completion:nil];
}
//恢复键盘
-(void)closeKeyboard:(NSNotification *)notification{
self.BottomLayoutConstaint.constant = 0;
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
UIViewAnimationOptions option = [notification.userInfo [UIKeyboardAnimationCurveUserInfoKey]intValue];
[UIView
animateWithDuration:duration delay:0
options:option animations:^{
[self.view
layoutIfNeeded];
} completion:nil];
}
//tableView滚动的时候自动收回键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[[[UIApplication
sharedApplication] keyWindow]
endEditing:YES];
}
如上面设置就可以实现键盘的上弹和收回了,当然要灵活运用。
//注册通知
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(openKeyboard:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(closeKeyboard:)
name:UIKeyboardWillHideNotification
object:nil];
}
//注销通知
-(void)viewDidDisAppear:(BOOL)animated{
[super viewDidDisappear:anima
4000
ted];
[[NSNotificationCenter
defaultCenter]removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter]removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
//键盘上弹
-(void)openKeyboard:(NSNotification *)notification{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
UIViewAnimationOptions option = [notification.userInfo [UIKeyboardAnimationCurveUserInfoKey]intValue];
// self.BottomLayoutConstaint.constant
这个是storyboard里面tableview的下沿,直接拉线的,而keyboardFrame.size.height是键盘的高度
self.BottomLayoutConstaint.constant = keyboardFrame.size.height+99*SizeScaleY;
[UIView
animateWithDuration:duration delay:0
options:option animations:^{
[self.view
layoutIfNeeded];
} completion:nil];
}
//恢复键盘
-(void)closeKeyboard:(NSNotification *)notification{
self.BottomLayoutConstaint.constant = 0;
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
UIViewAnimationOptions option = [notification.userInfo [UIKeyboardAnimationCurveUserInfoKey]intValue];
[UIView
animateWithDuration:duration delay:0
options:option animations:^{
[self.view
layoutIfNeeded];
} completion:nil];
}
//tableView滚动的时候自动收回键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[[[UIApplication
sharedApplication] keyWindow]
endEditing:YES];
}
如上面设置就可以实现键盘的上弹和收回了,当然要灵活运用。
相关文章推荐
- iOS学习——AutoLayout中利用 NSLayoutConstraint设置相对布局
- IOS之地图和定位应用开发
- iOS - CAEmitterLayer 学习笔记一
- iOS GPS定位CLLocationManager获取当前位置坐标 示例代码
- IOS项目结构
- IOS Constraints自动布局适应不同尺寸
- iOS 关于无限循环自动图片轮播器中的一种方法
- iOS9 xib加载正常,ios8.4加载缓慢
- iOS开发中总结的小技巧(持续更新中)
- iOS开发之同一个开发证书在多台电脑上使用
- iOS高德地图 初始化 重定义比例尺
- iOS 崩溃日志 Backtrace的符号化
- iOS之UNIX(iOS追根溯源)
- iOS多线程理解
- KVO的几个坑
- iOS应用程序的生命周期
- iOS开发之StackView
- iOS自动更新版本号脚本
- iOS开发之Masonry(四)
- iOS 同步GET