从iOS7开始,在我的UITableView顶部有额外的空间它有一个UITableViewStyleGrouped样式。

这里有一个例子:

tableview从第一个箭头开始,有35个像素的无法解释的填充,然后绿色的头是一个由viewForHeaderInSection返回的UIView(其中section为0)。

有人能解释一下这个35像素的数量是从哪里来的吗?我如何才能在不切换到UITableViewStylePlain的情况下摆脱它?


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

self.automaticallyAdjustsScrollViewInsets = NO;

试试吧,你能搞定的!

其他回答

当使用分组TableView时,使用这个来避免viewWillAppear中的边界切割

self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);

在iOS系统中,我在滚动视图中设置了tableview。当我在同一个屏幕上点击“返回”时滚动视图在顶部占用更多空间..为了解决这个问题,我使用了:

 self.automaticallyAdjustsScrollViewInsets = false

一个布尔值,指示视图控制器是否应自动调整其滚动视图嵌入。 默认值为true,该值允许视图控制器根据状态栏、导航栏、工具栏或选项卡栏所占用的屏幕区域来调整其滚动视图嵌入。如果您想自己管理滚动视图插入调整,例如当视图层次结构中有多个滚动视图时,则设置为false。

根据苹果iOS7的过渡指南,滚动视图的内容嵌入是自动调整的。 automcallyadjustsscrollviewinsets的默认值设置为YES。

拥有UITableView的UIViewController应该将这个属性设置为NO。

self.automaticallyAdjustsScrollViewInsets = NO;

这样就可以了。

编辑1:

也可以尝试

self.navigationController.navigationBar.translucent = YES;

这也删除了顶部的额外填充。

这是在iOS 11和Xcode 9.1中通过Storyboard轻松修复的方法:

Select Table View > Size Inspector > Content Insets: Never

2021年Xcode 12.3更新

要解决这个问题,需要禁用标准节页脚。 下面的代码片段可以完成这项工作:

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return nil
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0
}

的使用

tableView.contentInsetAdjustmentBehavior = .never

与此完全无关