从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 
}

其他回答

我得到了以下的帮助:

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

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

tableView.contentInsetAdjustmentBehavior = .never

是解决方案为我(iOS12 / xcode 10)在一个普通的表视图与头

如果自我。automcallyadjustsscrollviewinsets = NO;不为你工作,确保tableview头或section头的高度是CGFLOAT_MIN而不是0。

例如,如果你想让tableview头0高,你可以这样做:

    CGRect frame = self.tableViewHeader.frame;
    frame.size.height = CGFLOAT_MIN; //not 0
    self.tableView.tableHeaderView.frame = frame;
    self.tableView.tableHeaderView = self.tableViewHeader;

希望能有所帮助。

如果你在iOS 15或更高版本中遇到这个问题,请执行以下操作:

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