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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

对我来说,这就是帮助我的东西。我也支持ios6。

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

其他回答

所以我尝试了所有的方法,这次没有一个有用。我的案例是iOS 9上的分组表视图。我不知道为什么以及如何发现这个,但对我来说,用UIView设置tableViewHeader至少有0.01的高度是可行的。CGRectZero没有帮助,什么都没有帮助:

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

用这个吧,我觉得会有帮助…

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 {
    return 0.005f;// set this according to that you want...
 }

唯一对我有用的是:

迅速:

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

我遇到了同样的问题,但是tableView在tableView单元格中。 上面的无数答案都不起作用。 一个非常奇怪而简单的东西起了作用:

-在为tableView分配委托之前,放置估计的高度!!

    override func awakeFromNib() {
        super.awakeFromNib()

        tableView.estimatedRowHeight = 58
        tableView.estimatedSectionHeaderHeight = 45
        tableView.estimatedSectionFooterHeight = 10
        tableView.dataSource = self
        tableView.delegate = self
    }

将估计放在委托分配之后,导致我的内部tableView在标题上方有一个奇怪的空白。 我希望它能帮助到这里的人。

我假设这只是新UITableViewStyleGrouped样式的一部分。它存在于所有分组表视图中,似乎没有任何直接的方法来控制该空间。

如果这个空间是由一个UIView表示的,那么可以搜索UITableView的所有子视图来找到那个特定的视图并直接编辑它。然而,也有可能该空间只是标题和单元格开始之前的硬编码偏移量,没有任何方法来编辑它。

要搜索所有子视图(我将在表没有单元格时运行这段代码,以使它更容易阅读输出):

- (void)listSubviewsOfView:(UIView *)view {

    // Get the subviews of the view
    NSArray *subviews = [view subviews];

    // Return if there are no subviews
    if ([subviews count] == 0) return;

    for (UIView *subview in subviews) {

        NSLog(@"%@", subview);

        // List the subviews of subview
        [self listSubviewsOfView:subview];
    }
}