OGeek|极客世界-中国程序员成长平台

标题: ios - 当键盘出现iOS时向上滚动tableview行 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:04
标题: ios - 当键盘出现iOS时向上滚动tableview行

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

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

这是我的代码:

[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(keyboardOnScreen name:UIKeyboardDidShowNotification object:nil];

-(void)keyboardOnScreenNSNotification*)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 值有关!



Best Answer-推荐答案


使用此方法出现键盘

- (void)keyboardWasShownNSNotification *)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)keyboardWasHiddenNSNotification *)aNotification
    {

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4