【问题标题】:How to change UITableView content insets only if keyboard covers view?仅当键盘覆盖视图时如何更改 UITableView 内容插图?
【发布时间】:2015-10-30 22:11:38
【问题描述】:

我有一个包含 tableview 的视图控制器。我在我的应用程序的多个位置重用了这个视图控制器,因此它位于拆分视图控制器内部,也位于 iPad 上的弹出框内。

我添加了观察者来监听键盘显示/隐藏,并根据键盘高度更改我的内容插图。每当视图控制器是全屏高度时,这都有效,但它在弹出窗口中不起作用,弹出窗口调整大小并且 tableview 实际上没有被键盘覆盖,给我留下一堆空白空间。

有解决办法吗?如果键盘覆盖视图,如何仅更改内容插图?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    您只需要找到键盘将覆盖多少表格视图,然后将插图设置为多少。

    /* Get the keyboard rect (which is screen coordinates) in tableView coordinates */
    CGRect endFrameScreen = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect endFrameWindow = [tableView.window convertRect:endFrameScreen fromWindow:nil];
    CGRect endFrameView = [tableView convertRect:endFrameWindow fromView:nil];
    
    if (!CGRectIntersectsRect(tableView.bounds, endFrameView))
    {
        /* The keyboard will not cover our tableView; nothing to do */
        return;
    }
    
    /* This is how much we need to inset */
    CGFloat bottomInset = CGRectGetMaxY(tableView.bounds) - endFrameView.origin.y;
    

    如果仅使用 iOS8+,则可以使用 convertRect:fromCoordinateSpace:[[UIScreen mainScreen] coordinateSpace] 将两个 convertRect 步骤更改为一个步骤

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多