当键盘出现iOS时,滚动tableview排队

时间:2016-11-15 10:53:05

标签: ios objective-c uitableview uikeyboard

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

我正在使用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值有关!

1 个答案:

答案 0 :(得分:0)

使用此方法键盘出现

- (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;
    }
相关问题