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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

唯一对我有用的是:

迅速:

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

objective - c:

self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 0;

另外,我还有多余的空间来写第一部分。这是因为我错误地使用了tableHeaderView属性。修正了,以及添加:

self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 0.01))

其他回答

这是使用Swift 3的iOS 10的解决方案:

你可以通过实现UITableViewDelegate中的以下方法来去除顶部和底部填充。

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

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

我也一直在想这个问题。我很确定这是iOS7的漏洞。 最终帮助我的,是xib中的观点顺序。在一个视图中,表视图被正确显示,在另一个视图中,表视图有额外的35px空间。两者之间唯一的区别(UITableView)是,在显示不好的视图中,UITableView是第一个子,而在显示正常的视图中,它是第二个。

这对我来说很有用,只是改变了视图的顺序。我真的不喜欢为了解决问题而添加额外的代码。

我不确定这个答案是否正确,但它对我有用

我选择表视图,导航到标尺,改变Y值为零

只需将你的tableview(或使它开始)固定在视图的绝对顶部。多余的空间正好是导航条的高度。

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

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