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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

故事板:

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

代码:

self.automaticallyAdjustsScrollViewInsets = false

其他回答

我喝了和arielyz一样的药。一旦我把UITableView移到不是父视图的第一个子视图,它就消失了。空间是20px,不是35。

我无法在肖像xib中重现它,只能在风景xib中重现。如果我可以在一个简单的演示应用程序中重现它,我将稍后提交一个雷达漏洞。

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

我已经找到了我最初的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调用后重新绘制自己。

确保您已经为表提供了约束。我没有遇到过类似的问题,在桌子底部有一个无法解释的填充物。

我认为制作UIEdgeInsets -35 0 0 0是乏味的。在我的例子中,我实现了tableView: hightforheaderinsection:方法,它有可能返回0。

当我把0改成0.1f时,问题就消失了。