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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

根据苹果iOS7的过渡指南,滚动视图的内容嵌入是自动调整的。 automcallyadjustsscrollviewinsets的默认值设置为YES。

拥有UITableView的UIViewController应该将这个属性设置为NO。

self.automaticallyAdjustsScrollViewInsets = NO;

这样就可以了。

编辑1:

也可以尝试

self.navigationController.navigationBar.translucent = YES;

这也删除了顶部的额外填充。

其他回答

我检查了所有的答案。没有一个对我有效。我所要做的就是

self.myTableView.rowHeight = UITableViewAutomaticDimension
self.myTableView.estimatedRowHeight = 44.0

此外,这个问题并没有发生在tableView的顶部。它发生在tableView每个部分的顶部。

这个问题只发生在iOS9上。我们的应用在iOS10和iOS 11上运行良好。

我强烈推荐你看看这个很棒的问题和它的顶级答案:

在UITableView中使用自动布局进行动态单元格布局和可变行高

我们有很多答案。

1)你可以在view didload中添加UIImageview

UIImageView * imbBackground = [UIImageView new];
[self.view addSubview:imbBackground];

2)可以设置页眉和页脚高度为0.1

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

3)您可以添加高度为0.1的页眉和页脚视图

tblCampaigns.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tblCampaigns.bounds.size.width, 0.01f)];
tblCampaigns.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tblCampaigns.bounds.size.width, 0.01f)];

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()
}
set
  override func viewDidLoad() {
  self.tableView.delegate = self
  self.tableView.dataSource = self
}

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