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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

我有一个VC与容器在它(btw是一个导航控制器的一部分),并嵌入其中是一个TableVC。

选择我的TableVC,查看属性面板:

取消勾选自动调整滚动插入 取消检查顶部栏下的延伸边

然后我设置了容器相对于父视图的自动约束:

容器的观点。top = ParentView。顶边

这为我完成了以下任务:

保持桌面拉到顶部,但就在我的导航栏下面 刷新后,表返回到这个位置,而不是像以前那样在导航栏下向上+ 在实际的底部滚动表底部后,不缺它

我用这个设置进行了大量的试验和错误,但我最终发现了lekksi @移除UITableView单元格前的空白空间发布的这些小细节

其他回答

只是取消勾选“调整滚动视图嵌入”对我来说没用。 后来,我尝试将tableview的头设置为nil,幸运的是,这是有效的。

self.tableView.tableHeaderView = nil

如果你选择UITableViewStyleGrouped的样式,你需要实现Header和Footer高度委托方法,并且返回值需要大于0;是这样的:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

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

使用swift 4.1。试试这个:

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

故事板:

确保你的UITableView top约束没有说“Under top layout guide”。相反,它应该说“顶部空间:Superview”(Superview.Top)。

当然,在包含UITableView的UIViewController中,取消勾选“调整滚动视图Insets”。

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

与此完全无关