简体   繁体   English

编辑视图框和原点后,UITableView将不会滚动

[英]UITableView won't scroll after editing view frame and origin

I'm trying to implement a UITextView in a table cell at the bottom of a table view. 我正在尝试在表格视图底部的表格单元中实现UITextView。

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. 我在这里尝试了建议, 当选择了文本字段时使UITableView滚动 ,以及其他解决方案,但是它们有些不同,因为我必须人为地为当前视图添加额外的高度,以便为键盘创造空间。

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. 如果我尝试将表格视图的框架或范围(不是视图)保存在keyboardWillShow中,并将其还原到keyboardWillHide中,将恢复滚动,但是视图将被切成两半。

Are there any remedies to this besides hard-coding the additional height to the bottom of the view? 除了硬编码视图底部的额外高度之外,是否还有其他解决方法?

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. 另外,我通过在计算中使用tableview的contentSize属性实现了滚动到底部单元格。

-(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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM