ios键盘弹起tableView的滑动区域问题
2017-10-11 10:20
225 查看
在iOS项目开发过程中,经常需要点击textview弹起键盘,但弹起键盘后tableView的可见区域就变小了,如果这时tableView不能在可见区域内滑动,那tableView的内容就不能在键盘弹起的情况下完全显示,这样用户体验就不好了。下面提供一种解决方法。
由于大部分控制器类都需要用到tableView,为此可以写一个公共的父类BaseTableViewController继承自UIViewController,里面初始化tableView并实现其代理方法,设置一个属性
@property (nonatomic,
strong) UITapGestureRecognizer *tapGesture;
核心代码如下:
- (void)viewWillDisappear:(BOOL)animated {
[super
viewWillDisappear:animated];
[self.view
endEditing:YES];
[[NSNotificationCenter
defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc {
NSLog(@"dealloc %@", [self class]);
}
#pragma mark - Keyboard
- (void)keyboardWillShow:(NSNotification *)notification {
if (!self.tapGesture) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(viewTapped:)];
tap.cancelsTouchesInView =
NO;
[self.view
addGestureRecognizer:tap];
self.tapGesture = tap;
}
CGRect keyboardBounds = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
self.tableView.contentInset =
UIEdgeInsetsMake(self.tableView.contentInset.top,
0, keyboardBounds.size.height,
0);
}
- (void)keyboardWillHide:(NSNotification *)notification {
[self.view
removeGestureRecognizer:self.tapGesture];
self.tapGesture =
nil;
self.tableView.contentInset =
UIEdgeInsetsMake(self.tableView.contentInset.top,
0, 0,
0);
}
- (void)viewTapped:(id)sender {
[self.view
endEditing:YES];
}
这样子类在继承父类BaseTableViewController时,可在键盘弹起的情况下滑动tableView显示所有内容。
由于大部分控制器类都需要用到tableView,为此可以写一个公共的父类BaseTableViewController继承自UIViewController,里面初始化tableView并实现其代理方法,设置一个属性
@property (nonatomic,
strong) UITapGestureRecognizer *tapGesture;
核心代码如下:
- (void)viewWillDisappear:(BOOL)animated {
[super
viewWillDisappear:animated];
[self.view
endEditing:YES];
[[NSNotificationCenter
defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc {
NSLog(@"dealloc %@", [self class]);
}
#pragma mark - Keyboard
- (void)keyboardWillShow:(NSNotification *)notification {
if (!self.tapGesture) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(viewTapped:)];
tap.cancelsTouchesInView =
NO;
[self.view
addGestureRecognizer:tap];
self.tapGesture = tap;
}
CGRect keyboardBounds = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
self.tableView.contentInset =
UIEdgeInsetsMake(self.tableView.contentInset.top,
0, keyboardBounds.size.height,
0);
}
- (void)keyboardWillHide:(NSNotification *)notification {
[self.view
removeGestureRecognizer:self.tapGesture];
self.tapGesture =
nil;
self.tableView.contentInset =
UIEdgeInsetsMake(self.tableView.contentInset.top,
0, 0,
0);
}
- (void)viewTapped:(id)sender {
[self.view
endEditing:YES];
}
这样子类在继承父类BaseTableViewController时,可在键盘弹起的情况下滑动tableView显示所有内容。
相关文章推荐
- 解决TableView / ScrollView上的Menu问题(1滑出View区域还可点击2导致点击menu后View不能滑动)
- ios tableview section标题不滑动问题
- iOS中在tableview上通知控制键盘弹起界面上推的方法
- 解决tableview滑动时键盘隐藏回弹问题
- ios上tableview的区域随着手指滑动而动态改变
- iOS tableView滑动卡顿的问题之一
- iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法
- IOS中的frame 和 tableView的滑动问题
- ios 小问题,滑动tableview,导航出去,再回来会保持原样,但我想自动保持顶部
- iOS——tableView和适配、键盘弹出发送通知问题
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的问题
- iOStableView 自动布局常见问题之页面滑动到最底时,下面多出44的距离!
- ios tableView多个cell处理键盘遮挡内容视图的问题
- IOS中的frame 和 tableView的滑动问题
- IOS TableView滑动不灵敏问题
- iOS tableView 的 cell上其它控件重用(利用view的tag属性巧妙实现重用,从xib中加在cell重载问题)
- iOS之设置tableView背景图片/让UITableView自动滑动(定位)到某一行cell
- ios tableview 小问题总结
- ios7下tableview的背景色问题
- iOS-NSTimer在用户操作tableView时暂停的问题。