ios - 当键盘出现 iOS 时向上滚动 tableview 行

标签 ios objective-c uitableview uikeyboard

当键盘出现时,我的应用程序的一些 ui 组件从底部隐藏,实际上它是 tableView 的最后一行。

我正在使用 NSNotificationCenter 在键盘出现和消失时发出通知。

这是我的代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];

-(void)keyboardOnScreen:(NSNotification*)notification{
    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect screen = self.view.frame;        
    screen.size.height -= kbSize.height;
    float HeightOfToolbar =_inputbar.frame.size.height; //TOOLBAR
    float yPoint = screen.size.height-HeightOfToolbar;
    CGPoint scrollPt = CGPointMake(0, yPoint);
    [_dataTable setContentOffset:scrollPt animated:YES];
}

但是,tableview 向上滚动但不在正确的行,即键盘上方的最后一行。 有时它会滚动到倒数第二行,隐藏最后一行,或者更多。 与scrollPt值有关!

最佳答案

使用此方法键盘出现

- (void)keyboardWasShown:(NSNotification *)aNotification {
        NSDictionary* info = [aNotification userInfo];
        CGSize Size = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, Size.height - HeightOfToolbar, 0.0);//Set height according to key board height
        self.tableView.contentInset = contentInsets;
        self.tableView.scrollIndicatorInsets = contentInsets;
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }

隐藏键盘

 - (void)keyboardWasHidden:(NSNotification *)aNotification
    {

    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
    }

关于ios - 当键盘出现 iOS 时向上滚动 tableview 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40607988/

相关文章:

ios - 尝试创建每个工作日重复的 UILocalNotification

iphone - 通话结束后重新打开应用程序

ios - 属性中的强类型真的有必要吗?

iphone - 如何限制 NSFetchRequest 的结果数?

ios - 尝试在 Swift 应用程序中导入 MoPub 库时出错(错误 : use of undeclared type)

iphone - 当表格离开然后返回时,UITableView 单元格消失

iphone - 设置控件后面的 iOS 白色背景

ios - 使用 JBChartView 库的分组条形图

objective-c - 如何在不实际更改的情况下重新排列 char* 的开头和结尾(索引方式)?

ios - 在 iOS 中使用 Storyboard将图像设置为 TableView 的背景