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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

如果自我。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不喜欢头的高度为0.0。如果你要做的是有一个高度为0的标题,你可以跳到解决方案。 即使以后你给你的头分配了一个非0.0的高度,UITableView也不喜欢一开始就被分配一个高度为0.0的头。

解决方案:

然后,最简单可靠的修复方法是确保头高度在分配给表视图时不为0。

这样做是可行的:

// Replace UIView with whatever class you're using as your header below:
UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, CGFLOAT_MIN)];
self.tableView.tableHeaderView = tableViewHeaderView;

这样的事情会在某些时候(通常是在滚动之后)导致问题:

// Replace UIView with whatever class you're using as your header below:
UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.tableHeaderView = tableViewHeaderView;

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

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

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

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

2019答:

你只要这样做

tableView.contentInsetAdjustmentBehavior = .never

奇怪微妙的gotchya ->

现在表视图有一个非常奇怪的行为:

在有缺口(XR等)的设备上,它不会告诉你添加更多的插入,但只有当表格从屏幕的物理顶部开始时才会这样做。 如果你不是从屏幕顶部开始,它不会那样做,但是 这两种情况都是>>与safeAreaInsets .......无关这很让人困惑

所有这些都是完全没有记录的……你可以浪费几个小时来解决这个问题。

如果你确实需要从屏幕/表格的顶部开始测量,

事实上,简单地说:

tableView.contentInsetAdjustmentBehavior = .never

一个很好的例子是,当你在一个表格的顶部添加一些横幅或类似的东西时,这是现在很常见的,你只需要将表格的顶部插入设置为你的横幅/等在运行时的任何高度。

要做到这一点,必须使用

tableView.contentInsetAdjustmentBehavior = .never

电话:/

奖金陷阱

不要忘记,现在几乎总是动态加载一些信息(用户图片、描述等等),所以在信息到达之前不能将这些值设置为最终需要的值。另一个gotchya。:/

所以你会有这样的代码:

func setTableOffsetOnceFlagAreaSizeIsKnown() {
    tableView.contentInset.top = yourSpecialFlagViewUpTop.bounds.height
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    setTableOffsetOnceFlagAreaSizeIsKnown()
}

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

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