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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

扔我的十美分在-我有同样的问题后,我删除了一个标题单元格从表视图。为了解决这个问题,我将属性检查器中的TableView样式从“分组”更改为“普通”,并删除了额外的空间。

其他回答

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

这对我很管用。也可以根据section给出标题的高度。

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

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

我得到了以下的帮助:

YouStoryboard。>属性检查>取消勾选-调整滚动视图嵌入。

对于第一部分,我们需要像这样设置tableHeaderView:

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

对于其他section,我们应该像这样设置highightforfooterinsection:

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

对于iOS 15,在viewDidLoad中尝试以下解决方案:

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = .leastNormalMagnitude
}