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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

故事板:

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

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

其他回答

这些答案或变通办法都帮不了我。大多数都在处理tableView本身。引起问题的是我添加tableView或collectionView到的视图。这就是我解决问题的方法:

斯威夫特5

edgesForExtendedLayout = []

我把它放到viewDidLoad中,烦人的间隙消失了。希望这能有所帮助。

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

self.tableView.tableHeaderView = nil

如果你在iOS 15或更高版本中遇到这个问题,请执行以下操作:

if #available(iOS 15.0, *) {
    self.tableView.sectionHeaderTopPadding = 0
}

当你需要隐藏你的tableHeaderView时,你应该使用这段Swift 3代码:

tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0.0, height: CGFloat.leastNormalMagnitude)))

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 :)