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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

override func viewWillAppear(animated: Bool) {
        self.edgesForExtendedLayout = UIRectEdge.None

 //  OR

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

   //OR

 self.automaticallyAdjustsScrollViewInsets = false
        }

其他回答

self.automaticallyAdjustsScrollViewInsets = NO;

试试吧,你能搞定的!

当你使用UITableView.Style.grouped或UITableView.Style.InsetGrouped时,如果没有“tableview header”,tableview将自动添加一个顶部inset填充到“section header”,修复很简单:

 self.tableview.tableFooterView = UIView(frame: CGRect.init(origin: .zero, size: CGSize.init(width: tableview.frame.size.width, height: 1)))
 self.tableview.tableHeaderView = UIView(frame: CGRect.init(origin: .zero, size: CGSize.init(width: tableview.frame.size.width, height: 1)))

如果你使用uitableview。style。plain

self.tableview.contentInsetAdjustmentBehavior = .never

一般来说就足够了

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的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,再次创建它,奇怪的35px消失了。

它似乎有一个奇怪的错误在接口生成器。