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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

根据苹果的文档:

当将视图分配给此属性时,将该视图的高度设置为 一个非零值。表视图只尊重您的高度 视图的框架矩形;它调整你的头视图的宽度 自动匹配表视图的宽度。

斯威夫特5

如果你有一个分组UITableView,你想要一个tableHeaderView:

在将视图分配给tableView之前。tableHeaderView,设置该视图的高度为一个非零值,例如:

// Set the initial height of your custom view to the smallest value  
myTableHeaderView.frame.size.height = .leastNonzeroMagnitude

// Assign your custom view to the header view
tableView.tableHeaderView = myTableHeaderView

// Set width constraint, height should grow based on your view. 
tableView.tableHeaderView!.widthAnchor.constraint(equalTo: tableView.widthAnchor).isActive = true

不需要使用contentInsets或处理委托方法。

其他回答

我对它做了一些改动这似乎是设置tableView的tableHeaderView = nil的副作用。

因为我的tableView有一个动态出现的tableHeaderView,当我需要隐藏tableHeaderView,而不是做self.tableView.tableHeaderView = nil;,我做:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];

我更喜欢这个解决方案,而不是设置一个有点随意的contentInset。因为我使用了contentInset。也是动态Top。当我重新计算contentInset时,必须记得删除额外的35px。顶部很乏味。

这段代码对我来说很有用,对我来说最好的答案是用objective-C写的,所以我把它转换成Swift。

针对Swift 4.0+

self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: .leastNonzeroMagnitude))

只需将其写入viewDidLoad(),它就会像一个咒语一样工作。

对于iOS 15+,上面的一个将不起作用,所以使用这个:-

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

对于iOS 15+,如果你想为你的整个项目应用更改,那么使用这个:-

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    if #available(iOS 15.0, *) {
        UITableView.appearance().sectionHeaderTopPadding = 0.0
    }
}

我已经找到了我最初的bug的原因,并创建了一个示例项目来展示它。我相信有一个iOS7漏洞。

在iOS7中,如果你用分组样式创建一个UITableView,但是在第一个布局上没有设置一个委托,然后你设置了一个委托并调用reloadData,在顶部会有一个35px的空间,永远不会消失。

看这个项目我做的展示bug: https://github.com/esilverberg/TableViewDelayedDelegateBug

特别是这个文件:https://github.com/esilverberg/TableViewDelayedDelegateBug/blob/master/TableViewDelayedDelegateBug/ViewController.m

如果第24行是活动的,

[self performSelector:@selector(updateDelegate) withObject:nil afterDelay:0.0];

在顶部会有一个额外的35px空间。如果第27行是活动的,第24行被注释掉,

self.tableView.delegate = self;

顶部没有空间。这就像tableView在某处缓存结果而不是在委托设置和reloadData调用后重新绘制自己。

这是使用Swift 3的iOS 10的解决方案:

你可以通过实现UITableViewDelegate中的以下方法来去除顶部和底部填充。

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

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

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