要实现这个功能 大家都知道监听键盘的响应事件是最好的
步骤1
监听键盘的响应和失去响应的两个事件
代码如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
步骤2
实现监听的两个方法
代码如下:
- (void)keyboardWillShow:(NSNotification *)info { CGRect keyboardBounds = [[[info userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; _tableView.contentInset = UIEdgeInsetsMake(_tableView.contentInset.top, 0, keyboardBounds.size.height, 0); } - (void)keyboardWillHide:(NSNotification *)info { _tableView.contentInset = UIEdgeInsetsMake(_tableView.contentInset.top, 0, 0, 0); }
步骤3
移除上面的两个监听事件
代码如下:
- (void)viewDidDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }
去试一下吧 很好用的一个小功能
时间: 2024-11-01 13:57:14