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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

Swift 4代码: 对于没有section header的tableview,你可以添加以下代码:

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

你会得到标题间距为0。

如果你想要一个特定高度的头文件,传递该值:

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

和viewForHeaderinSection委托的视图。

其他回答

如果自我。automcallyadjustsscrollviewinsets = NO;不为你工作,确保tableview头或section头的高度是CGFLOAT_MIN而不是0。

例如,如果你想让tableview头0高,你可以这样做:

    CGRect frame = self.tableViewHeader.frame;
    frame.size.height = CGFLOAT_MIN; //not 0
    self.tableView.tableHeaderView.frame = frame;
    self.tableView.tableHeaderView = self.tableViewHeader;

希望能有所帮助。

尝试改变UITableView从UIScrollView继承的contentInset属性。

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

这是个变通办法,但很有效

故事板:

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

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

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

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

唯一对我有用的是:

迅速:

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