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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

set
  override func viewDidLoad() {
  self.tableView.delegate = self
  self.tableView.dataSource = self
}

其他回答

当你使用UITableView.Style.grouped或UITableView.Style.InsetGrouped时,如果没有“tableview header”,tableview将自动添加一个顶部inset填充到“section header”,修复很简单:

 self.tableview.tableFooterView = UIView(frame: CGRect.init(origin: .zero, size: CGSize.init(width: tableview.frame.size.width, height: 1)))
 self.tableview.tableHeaderView = UIView(frame: CGRect.init(origin: .zero, size: CGSize.init(width: tableview.frame.size.width, height: 1)))

如果你使用uitableview。style。plain

self.tableview.contentInsetAdjustmentBehavior = .never

一般来说就足够了

None of the previous answers worked for me. What did end up working for my situation (which was seeing that padding after dynamically adding a header to my Table View) was selecting the Navigation Bar from the Document Outline and rechecking the Translucent box (contrary to some answers that were saying to force it to be translucent). I had previously unchecked it because it had made my red look a little orange, but I could not try the solution of reordering the elements in the scene since the Table View was the only element in this scene. This solution works as long as you are okay with the Navigation Bar's color having a translucent layer over it. Hope this helps :)

我曾经遇到过一个额外的顶部空间的问题,我必须把一堆答案串起来才能得到解决方案。(5小时检查所有答案)

首先,如果你关心

表格视图模式为“分组”,请将其更改为“普通”

不需要对页眉或页脚部分做任何更改,也不需要添加与它们相关的额外委托方法

然后在InterfaceBuilder中有TableView的ViewController中 —取消勾选“调整滚动视图插入” -取消检查扩展边缘:在顶部条下

*此外,确保你删除了衍生数据,并在模拟器或手机中重新安装应用程序,以有效地反映所做的更改。

UI更改有时不反映,因为IDE也…

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.0
}

这对我很管用。也可以根据section给出标题的高度。

如果我添加一个高度很小的tableHeaderView,它就能解决这个问题

    let v = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.000000001))
    tableViewDocuments.tableHeaderView = v