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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

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

我选择表视图,导航到标尺,改变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))

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

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

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

iOS 15的解决方案:

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

修正:在整个项目中修正:

if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = 0
}

更多详细信息:在iOS 15中,表视图头部上有额外的填充

注意:这只适用于UITableView.Style.plain。

我们有很多答案。

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)];
override func viewWillAppear(animated: Bool) {
        self.edgesForExtendedLayout = UIRectEdge.None

 //  OR

self.sampleTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);

   //OR

 self.automaticallyAdjustsScrollViewInsets = false
        }