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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

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

self.edgesForExtendedLayout = UIRectEdgeNone;

你的问题似乎和我的相似

更新:

iOS 9.x中的Swift:

self.edgesForExtendedLayout = UIRectEdge.None

Swift 3:

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

其他回答

如果自我。automcallyadjustsscrollviewinsets = NO;不为你工作,确保tableview头或section头的高度是CGFLOAT_MIN而不是0。

例如,如果你想让tableview头0高,你可以这样做:

    CGRect frame = self.tableViewHeader.frame;
    frame.size.height = CGFLOAT_MIN; //not 0
    self.tableView.tableHeaderView.frame = frame;
    self.tableView.tableHeaderView = self.tableViewHeader;

希望能有所帮助。

在ios7中,你的视图开始于屏幕的上方,而不是导航栏的下方。它非常简单的设置到底部的导航栏。检查这个答案的解决方案,这不是同一个问题,但非常相关的你。

故事板:

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

代码:

self.automaticallyAdjustsScrollViewInsets = false

故事板:

确保你的UITableView top约束没有说“Under top layout guide”。相反,它应该说“顶部空间:Superview”(Superview.Top)。

当然,在包含UITableView的UIViewController中,取消勾选“调整滚动视图Insets”。

我们有很多答案。

1)你可以在view didload中添加UIImageview

UIImageView * imbBackground = [UIImageView new];
[self.view addSubview:imbBackground];

2)可以设置页眉和页脚高度为0.1

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

3)您可以添加高度为0.1的页眉和页脚视图

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