UITableView won't scroll after editing view frame and origin

孤街醉人 提交于 2020-01-22 02:18:05

问题


I'm trying to implement a UITextView in a table cell at the bottom of a table view.

I've tried the suggestions here Making a UITableView scroll when text field is selected, and other solutions as well, but they're a bit different because I have to artificially add extra height to the current view in order to create space for the keyboard.

Here's what I added to the previous solution in order to port it to my app.

-(void) keyboardWillShow:(NSNotification *)note {
      CGRect frame = self.view.frame;
      frame.size.height += keyboardHeight;
      frame.origin.y -= keyboardHeight;
        self.view.frame = frame;
}

-(void) keyboardWillHide:(NSNotification *)note
{
        CGRect frame = self.view.frame;
        frame.size.height -= keyboardHeight;
    frame.origin.y += keyboardHeight;

}

Doing this will correctly add the height to the view and scroll to the cell, but after restoring the original view's height, scrolling beyond the current visible view becomes impossible, even though there is valid content outside of the boundaries (I see the text view before the scroll bar bounces back).
If I try to save the tableview's frame or bounds (not the view) in keyboardWillShow and restore them in keyboardWillHide, the scrolling will be restored, but the view will be cut in half.

Are there any remedies to this besides hard-coding the additional height to the bottom of the view?


回答1:


I was able to solve my problem of the locked scrolling by removing the code that edits the view's origin. In addition, I implemented scrolling to the bottom cell by using the tableview's contentSize property in my calculations.

-(void) keyboardWillShow:(NSNotification *)note
{

  if(!isKeyboardShowing)
    {
    isKeyboardShowing = YES;
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
    CGFloat keyboardHeight = keyboardBounds.size.height;

            CGRect frame = self.view.frame;
            frame.size.height += keyboardHeight;
            self.view.frame = frame;

    CGPoint scrollPoint = frame.origin;
    scrollPoint.y += _tableView.contentSize.height - keyboardHeight;
    [_tableView setContentOffset:scrollPoint animated:YES];
    }
}


来源:https://stackoverflow.com/questions/1842560/uitableview-wont-scroll-after-editing-view-frame-and-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!