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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

唯一对我有用的是:

迅速:

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

其他回答

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

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

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

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

我对它做了一些改动这似乎是设置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。顶部很乏味。

对我来说,我尝试了所有的解决方案,但我在考虑为UITableView部分设置高度,它为我工作。(使用斯威夫特)

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }
override func viewWillAppear(animated: Bool) {
        self.edgesForExtendedLayout = UIRectEdge.None

 //  OR

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

   //OR

 self.automaticallyAdjustsScrollViewInsets = false
        }