底部的 UITableView 额外 space

UITableView extra space at bottom

我的 tableview 底部有一个额外的 space,如图所示:

tableView 中所有行的高度都固定为 71pt。

好的,我知道了。

"Adjust Scroll View Insets" 标记在我的 viewController 上(不是我的 tableView)。

这里有一个answer有详细的解释

在我的例子中,这是因为 UITableView 的底部默认有一个高度为 18 的页脚。将其设置为 1 会删除底部多余的 space。

当我将我的 UITableView 上下翻转以显示聊天消息屏幕时,我遇到了这个问题。

在我翻转 table 视图之前,内容插图在顶部留有空白 space 这样当我们一直向上滚动时,内容不会被导航栏遮挡.一旦我翻转它,插图就会移到底部。

在 UITableView 的 Size Inspector 中将 'Content Insets' 更改为 Never 以在最后摆脱此 space。

这是通过 Storyboard 轻松修复它的方法 (iOS 11):

Select Table 查看 > 大小检查器 > 内容插入:从不

在我的案例中,表视图是通过编程方式创建的,我必须设置 tableView.estimatedRowHeight 以使 space 消失

如果有嵌入式导航控制器,选中 "shows toolbar" 会在视图控制器中创建底部间距。取消选中即可。

在这种情况下,您可以将表视图的底部 space 0 赋给它的超级视图,我认为它会起作用,或者您可以做到,

tableView.tableFooterView = UIView();

或者你可以像下面那样做,

下限值应为 1.0。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
    if(section == 0) {
       return 6;
    } else {
       return 1.0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
    return 5.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

@urgentx 的 完成了大部分我想要的,但没有完全实现。我处于类似的情况(聊天应用程序的 UITableView 上下颠倒)但是他们的建议完全禁用安全区域行为,这在 iPhone X 这样的设备上是不可取的,因为这意味着 tableview 将被剪裁主页指示器和任何顶部导航栏。

为了使 tableview 在倒置时仍能正确避开安全区域,我做了以下操作:

override func viewDidLoad() {
    super.viewDidLoad()
    self.automaticallyAdjustsScrollViewInsets = false
    self.tableView.contentInsetAdjustmentBehavior = .never
}

然后:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    //fetch the safe area in layoutSubviews since this it's possible that they can change when the device is rotated
    let safeArea = self.tableView.safeAreaInsets
    //insets are **flipped** because the tableview is flipped over its Y-axis!
    self.tableView.contentInset = .init(top: safeArea.bottom, left: 0, bottom: safeArea.top, right: 0)
}

这在本质上复制了 contentInsetAdjustmentBehavior = .automatic 行为,但插图翻转了 Y-axis。

如果涉及 iOS 11 和 Xcode 9,则公认的解决方案不再是正确的。要达到类似的效果,您必须转到 Size Inspector,您可以在其中找到:

您可以在代码中实现相同的功能:

if #available(iOS 11.0, *) {
    scrollView.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}

我遇到了同样的问题,最新的 Xcode (11.3) 和最新的 iOS (13.3) iPhone。我尝试了很多答案,但 none 有效。

我的 tableView 有顶部、底部、前导和尾部约束,但不知何故在最后一个单元格下方的底部有很大的 space。 (tableView.contentSize.height 几乎是所需数据的两倍)tableView 的数据在 all.It 处不是动态的,这在显示它的视图控制器和隐藏 tableView 时都会发生然后再次显示。

我通过如下方式使用scrollView的委托方法解决了这个问题,这样当用户开始拖动时,高度就会被调整。对我来说,效果很好,希望你也一样!

// this is the best place to adjust the contentSize.height of tableView.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    print("scrollViewWillBeginDragging():") // optional
    // replace allParkingGeos.count with your data for tableView
    tableView.contentSize.height = CGFloat(allParkingGeos.count) * cellHeight
}

答案并没有解决我的问题。 我有一个 UISearchbar 作为 tableHeaderView 导致白色 space (我不知道为什么)。我从情节提要中的 table 视图中删除了搜索栏,并在 table header 中添加了 viewWillLayoutSubviews,然后问题就解决了。

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tableView.tableHeaderView = searchBar
}