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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

自我。automcallyadjustsscrollviewinsets = NO;

这在iOS 11中已弃用,所以你应该使用new属性 contentInsetAdjustmentBehavior在你的代码中,它应该修复这个问题。

if #available(iOS 11.0, *) {
    collectionView.contentInsetAdjustmentBehavior = .never
}

在UIScrollView中添加了一个名为contentInsetAdjustmentBehavior的新属性,用于确定调整内容偏移量

其他回答

我曾经遇到过一个额外的顶部空间的问题,我必须把一堆答案串起来才能得到解决方案。(5小时检查所有答案)

首先,如果你关心

表格视图模式为“分组”,请将其更改为“普通”

不需要对页眉或页脚部分做任何更改,也不需要添加与它们相关的额外委托方法

然后在InterfaceBuilder中有TableView的ViewController中 —取消勾选“调整滚动视图插入” -取消检查扩展边缘:在顶部条下

*此外,确保你删除了衍生数据,并在模拟器或手机中重新安装应用程序,以有效地反映所做的更改。

UI更改有时不反映,因为IDE也…

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

self.edgesForExtendedLayout = UIRectEdgeNone;

你的问题似乎和我的相似

更新:

iOS 9.x中的Swift:

self.edgesForExtendedLayout = UIRectEdge.None

Swift 3:

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

根据苹果iOS7的过渡指南,滚动视图的内容嵌入是自动调整的。 automcallyadjustsscrollviewinsets的默认值设置为YES。

拥有UITableView的UIViewController应该将这个属性设置为NO。

self.automaticallyAdjustsScrollViewInsets = NO;

这样就可以了。

编辑1:

也可以尝试

self.navigationController.navigationBar.translucent = YES;

这也删除了顶部的额外填充。

下面的操作(Swift)解决了这个问题,但当你不需要头文件的时候,这是有效的。

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

如果你这样做,你将不得不放弃第一部分,并使用其他内容。

UITableViewDataSource实施:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return <number_of_data_sections>+1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // the first section we don't use for data
    if section == 0 {
        return 0
    }

    // starting from 1, there are sections we use
    if section == 1 { 
        let dataSection = section - 1

        // use dataSection for your content (useful, when data provided by fetched result controller). For example:
       if let sectionInfo = myFRC!.sections![dataSection] as? NSFetchedResultsSectionInfo {
            return sectionInfo.numberOfObjects
        }
    }

    return 0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let dataIndexPath = NSIndexPath(forRow: indexPath.row, inSection: (indexPath.section - 1) )
    // return cell using transformed dataIndexPath
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 1 {
        // return your header height
    }
    return CGFloat.min
}


func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 1 {
        // return your header view
    }
    return nil
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    // in my case, even when 1st section header was of zero heigh, I saw the space, an that was a footer. I did not need footer at all, so always gave zero height
    return CGFloat.min
}

就是这样。模型不知道任何变化,因为我们在访问数据时转换了节号。

只需将你的tableview(或使它开始)固定在视图的绝对顶部。多余的空间正好是导航条的高度。