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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

感谢@Aurelien Porte的回答。这是我的解决方案

产生此问题的原因:-

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

在ViewDidLoad: -

self.edgesForExtendedLayout = UIRectEdge.None

self.automaticallyAdjustsScrollViewInsets = false

不需要这样的东西:-

self.myTableview.contentInset = UIEdgeInsetsMake(-56, 0, 0, 0)

在highforheaderinsection委托中:-

if section == 0
    {
        return 1
    }
    else
    {
        return 40; // your other headers height value
    }

在viewForHeaderInSection委托中:-

if section == 0 
{  
   // Note CGFloat.min for swift
   // For Objective-c CGFLOAT_MIN 
   let headerView = UIView.init(frame: CGRectMake(0.0, 0.0, self.myShaadiTableview.bounds.size.width, CGFloat.min)) 
   return headerView
}
else
{ 
   // Construct your other headers here 
}

其他回答

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

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

故事板:

确保你的UITableView top约束没有说“Under top layout guide”。相反,它应该说“顶部空间:Superview”(Superview.Top)。

当然,在包含UITableView的UIViewController中,取消勾选“调整滚动视图Insets”。

感谢@Aurelien Porte的回答。这是我的解决方案

产生此问题的原因:-

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

在ViewDidLoad: -

self.edgesForExtendedLayout = UIRectEdge.None

self.automaticallyAdjustsScrollViewInsets = false

不需要这样的东西:-

self.myTableview.contentInset = UIEdgeInsetsMake(-56, 0, 0, 0)

在highforheaderinsection委托中:-

if section == 0
    {
        return 1
    }
    else
    {
        return 40; // your other headers height value
    }

在viewForHeaderInSection委托中:-

if section == 0 
{  
   // Note CGFloat.min for swift
   // For Objective-c CGFLOAT_MIN 
   let headerView = UIView.init(frame: CGRectMake(0.0, 0.0, self.myShaadiTableview.bounds.size.width, CGFloat.min)) 
   return headerView
}
else
{ 
   // Construct your other headers here 
}

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

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

    override func awakeFromNib() {
        super.awakeFromNib()

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

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

故事板:

只需取消勾选:在视图控制器的选项中调整滚动视图嵌入

代码:

self.automaticallyAdjustsScrollViewInsets = false