首先必须在页面显示时加上键盘通知事件
- (void)viewWillAppear:(BOOL)animated
{
// 设置键盘显示和隐藏通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
当然不要忘记在页面显示时把事件注销掉
- (void)viewWillDisappear:(BOOL)animated
{
// 注销键盘显示和隐藏通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
因为需要让TableView的显示区域缩小,所以要要使得TableView可以滑动,顺便看到滚动条
_tableView.scrollEnabled = YES;
然后就是键盘的通知事件了
因为需要动画与键盘弹上同步所以需要键盘弹上的时间,同时获取键盘的Rect
CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
然后计算键盘的偏移量UIEdgeInsets
如果你的TableView的底部离View的还有距离,则需要见键盘的高度上减去此距离
NSInteger distance = 13;
UIEdgeInsets e = UIEdgeInsetsMake(0, 0, bounds.size.height-distance, 0);
再加上动画效果
[UIView animateWithDuration:duration animations:^{
[_tableView setScrollIndicatorInsets:e];
[_tableView setContentInset:e];
}];
那么TableView的显示区域就设置好了,当然在取消键盘的时候TableView需要复原
-(void)keyboardWillHide:(NSNotification *)notification
{
CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration animations:^{
[_tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
[_tableView setContentInset:UIEdgeInsetsZero];
}];
_tableView.scrollEnabled = NO;
}
看看效果发现TableView虽然可视区域正确,但是点击被键盘覆盖部分的Cell中的TextFiled却不会自动上弹,这是需要一个标记你所点击的TextFiled所在Cell的Rect
故此定义一个全局变量
CGRect m_InputRect; // 所选中的TextFiled的Rcct
设置textFiled的代理为self,并加上代理UITextFieldDelegate
cell.textContext.delegate = self;
实现代理,目的就是找到TextFiled所在的Cell并且取得他的Rect,然后赋值给m_InputRec
<span style="font-size:24px">#pragma mark -UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
m_InputRect = CGRectZero;
UIView* parent = textField.superview;
while (parent) {
if ([parent isKindOfClass:[UITableViewCell class]]) {
m_InputRect = parent.frame;
break;
}
parent = parent.superview;
}
}</span>
所以最后在keyboardWillShow中加上
[_tableView scrollRectToVisible:m_InputRect animated:YES];
即最后keyboardWillShow的实现为:
#pragma mark - Keyboard Notification
-(void)keyboardWillShow:(NSNotification *)notification
{
CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIEdgeInsets e = UIEdgeInsetsMake(0, 0, bounds.size.height - 13, 0);
[UIView animateWithDuration:duration animations:^{
[_tableView setScrollIndicatorInsets:e];
[_tableView setContentInset:e];
[_tableView scrollRectToVisible:m_InputRect animated:YES];
}];
_tableView.scrollEnabled = YES;
}
注明:如需转载请注明出处http://blog.youkuaiyun.com/dengbin9009/article/details/12706633,另发现不合理的地方还望大家即时指出。
本文将介绍如何在使用Swift进行iOS应用开发时,当键盘弹出时自动调整TableView的滚动区域,确保用户仍然能够方便地与应用交互。通过实现键盘显示和隐藏的通知事件,我们能够计算键盘的高度并相应地调整滚动视图的显示区域,同时保持对滚动条的可见性。此外,文章还将讨论如何在键盘隐藏时复原TableView的状态,以及如何标记用户点击的文本字段所在的单元格矩形,从而在键盘覆盖时仍能定位到正确的输入位置。
6269

被折叠的 条评论
为什么被折叠?



