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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

Swift3

如果你使用分组表,然后做以下事情。 这将删除顶部空间。

    var frame = CGRect.zero
    frame.size.height = .leastNormalMagnitude
    self.tableView.tableHeaderView = UIView(frame: frame)
    self.tableView.tableFooterView = UIView(frame: frame)

如果你仍然得到这个问题,那么它将删除默认的内容偏移从顶部。

[永不设置内容插入] https://i.stack.imgur.com/fjxOV.png

其他回答

在尝试了一切之后,终于知道如何解决这个问题!

我注意到,由于某种原因,我的tableview的contenttoffset有一个'y'为-20,所以当配置我的tableview: 斯威夫特3/4

tableView.contentOffset = .zero

objective - c

self.tableView.contentOffset = CGPointMake(0, 44);

你可以检测你的应用是否运行iOS7或更高版本,并在你的表视图委托中添加这两个方法(通常在你的UIViewController代码中)

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

这可能不是一个优雅的解决方案,但对我来说是可行的

斯威夫特版本:

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

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

对于ios7,如果你在视图控制器中分配tableview,你可以查看

self.edgesForExtendedLayout = UIRectEdgeNone;

你的问题似乎和我的相似

更新:

iOS 9.x中的Swift:

self.edgesForExtendedLayout = UIRectEdge.None

Swift 3:

self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)

简单地在VC中添加以下到你的viewDidLoad:

self.automaticallyAdjustsScrollViewInsets = NO;

Swift 4代码: 对于没有section header的tableview,你可以添加以下代码:

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

你会得到标题间距为0。

如果你想要一个特定高度的头文件,传递该值:

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

和viewForHeaderinSection委托的视图。