编辑视图框和原点后,UITableView不会滚动 [英] UITableView won't scroll after editing view frame and origin

查看:115
本文介绍了编辑视图框和原点后,UITableView不会滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表格视图底部的表格单元格中实现UITextView。

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

我在这里尝试了建议选择文本字段时创建UITableView滚动,以及其他解决方案,但它们有点不同因为我必须人为地为当前视图添加额外的高度才能为键盘创建空间。

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;

}

这样做会正确地将高度添加到视图并滚动到单元格,但在恢复原始视图的高度后,滚动超出当前可见视图变得不可能,即使边界外有有效内容(我在滚动条反弹之前看到文本视图)。

如果我尝试在keyboardWillShow中保存tableview的框架或边界(不是视图)并在keyboardWillHide中恢复它们,则会恢复滚动,但视图将减少一半。

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?

推荐答案

通过删除编辑视图原点的代码,我能够解决锁定滚动的问题。另外,我在计算中使用tableview的contentSize属性实现了向底部单元格的滚动。

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

这篇关于编辑视图框和原点后,UITableView不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆