为什么我的UITableView顶部有额外的填充,iOS7中的样式为UITableViewStyleGrouped

时间:2013-09-18 19:07:07

标签: ios uitableview ios7

从iOS7开始,UITableView顶部有一个额外的空格,其格式为UITableViewStyleGrouped

以下是一个例子:

enter image description here

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

任何人都可以解释这35个像素的数量来自何处以及如何在不切换到UITableViewStylePlain的情况下摆脱它?

70 个答案:

答案 0 :(得分:856)

我得到了以下方面的帮助:

YouStoryboard.storyboard> YouViewController>属性检查器>取消选中 - 调整滚动视图插图。

enter image description here

答案 1 :(得分:300)

我更多地玩了一下,看起来这是设置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.top更喜欢这个解决方案,因为我也动态使用contentInset.top。每当我重新计算contentInset.top时,必须记住删除额外的35px是乏味的。

答案 2 :(得分:177)

对于IOS 7,如果要在视图控制器中分配tableview,可以查看

self.edgesForExtendedLayout = UIRectEdgeNone;

你的问题似乎与我的相似

更新:

iOS 9.x中的Swift:

self.edgesForExtendedLayout = UIRectEdge.None

斯威夫特3:

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

答案 3 :(得分:164)

尝试更改contentInsetUITableView继承的UIScrollView媒体资源。

self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);

这是一种解决方法,但它可以正常工作

答案 4 :(得分:132)

self.automaticallyAdjustsScrollViewInsets = NO;

试试,你可以处理它!

答案 5 :(得分:70)

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

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

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

这可能不是一个优雅的解决方案,但对我有用

Swift版本:

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

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

答案 6 :(得分:52)

我找到了原始bug的原因并创建了一个展示它的示例项目。我相信有一个iOS7错误。

从iOS7开始,如果您使用Grouped样式创建UITableView,但在第一个布局上没有设置委托,那么您设置委托并调用reloadData,顶部将有一个35px的空间永远不会去远。

请参阅此项目,我展示了错误:https://github.com/esilverberg/TableViewDelayedDelegateBug

特别是此文件:https://github.com/esilverberg/TableViewDelayedDelegateBug/blob/master/TableViewDelayedDelegateBug/ViewController.m

如果第24行有效,

[self performSelector:@selector(updateDelegate) withObject:nil afterDelay:0.0];

顶部会有额外的35像素空间。如果第27行处于活动状态且24被注释掉,

self.tableView.delegate = self;

顶部没有空间。这就像tableView在某处缓存结果而不是在设置委托并调用reloadData后重绘自身。

答案 7 :(得分:49)

取消选中“调整滚动视图插入内容”

enter image description here

答案 8 :(得分:45)

另一个快速评论......即使在XCode 6.1中,也有一个错误,在UIScrollViewsUITextViewsUITableViews的顶部显示了垂直空格。

enter image description here

有时,修复此问题的唯一方法是进入故事板并拖动问题控件,使其不再是页面上的第一个子视图。

enter image description here

(感谢 Oded 指示我朝这个方向发展...我发布此评论,只是为了添加一些屏幕截图,以展示症状并进行修复。)

答案 9 :(得分:40)

根据Apple的这个transition guide for iOS7,滚动视图的内容插入会自动调整。 automaticAdjustsScrollViewInsets 的默认值设置为YES。

具有UITableView的UIViewController应将此属性设置为NO。

self.automaticallyAdjustsScrollViewInsets = NO;

这样就可以了。

编辑1:

另外,可以试试 -

self.navigationController.navigationBar.translucent = YES;

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

答案 10 :(得分:38)

使用分组TableView时,请使用此方法避免在viewWillAppear

中进行边框切割
self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);

答案 11 :(得分:34)

上面的许多答案都太过于苛刻。如果Apple决定解决这一意外行为,他们将在未来的任何时候破解。

问题的根源:

  1. UITableView不喜欢高度为0.0的标头。如果你要做的是要有一个高度为0的标题,你可以跳到解决方案。

  2. 即使稍后您为标题指定了非0.0高度,UITableView也不希望首先为其指定高度为0.0的标题。

  3. <强>解决方案:

    然后,最简单可靠的解决方法是确保在将表头视图分配给表视图时标题高度不为0.

    这样的事情会起作用:

    // Replace UIView with whatever class you're using as your header below:
    UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, CGFLOAT_MIN)];
    self.tableView.tableHeaderView = tableViewHeaderView;
    

    这样的事情会在某些时候导致问题(通常是在滚动之后):

    // Replace UIView with whatever class you're using as your header below:
    UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = tableViewHeaderView;
    

答案 12 :(得分:29)

<强>故事板:

在View Controller的选项

中取消选中:text-left

enter image description here

<强>代码:

Adjust Scroll View Insets

答案 13 :(得分:20)

这是使用Swift 3的iOS 10解决方案:

您可以通过UITableViewDelegate实施以下方法来摆脱顶部和底部填充。

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

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

答案 14 :(得分:14)

只需将以下内容添加到VC中的viewDidLoad:

self.automaticallyAdjustsScrollViewInsets = NO;

答案 15 :(得分:14)

就我而言,这对我有所帮助。我也支持ios6。

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

答案 16 :(得分:14)

所以我在这里尝试了每一种方法,而这一次都没有帮助。我的案例是iOS 9上的分组表格视图。我真的不知道为什么以及如何找到这个,但对我来说,将tableViewHeader设置为UIView至少{{1高度解决了。 0.01没有帮助,没有任何帮助:

CGRectZero

答案 17 :(得分:10)

Swift:iOS我在滚动视图上有桌面视图..当我在同一个屏幕上点击“返回”时。滚动视图在顶部占用更多空间..解决这个我用过:

 self.automaticallyAdjustsScrollViewInsets = false

一个布尔值,指示视图控制器是否应自动调整其滚动视图插入。 默认值为true,允许视图控制器调整其滚动视图插入以响应状态栏,导航栏和工具栏或选项卡栏消耗的屏幕区域。如果要自己管理滚动视图插入调整,则设置为false,例如视图层次结构中有多个滚动视图时。

答案 18 :(得分:10)

感谢@Aurelien Porte的回答。这是我的解决方案

此问题的原因: -

  1. UITableView不喜欢有一个高度为0.0的标题。如果您尝试做的是拥有高度为0的标题,则可以跳转到解决方案。
  2. 即使稍后您为标题指定了非0.0高度,UITableView也不希望首先为其指定高度为0.0的标题。
  3. 在ViewDidLoad中: -

    self.edgesForExtendedLayout = UIRectEdge.None
    
    self.automaticallyAdjustsScrollViewInsets = false
    

    不需要这样的东西: -

    self.myTableview.contentInset = UIEdgeInsetsMake(-56, 0, 0, 0)
    

    heightForHeaderInSection委托中: -

    if section == 0
        {
            return 1
        }
        else
        {
            return 40; // your other headers height value
        }
    

    viewForHeaderInSection委托中: -

    if section == 0 
    {  
       // Note CGFloat.min for swift
       // For Objective-c CGFLOAT_MIN 
       let headerView = UIView.init(frame: CGRectMake(0.0, 0.0, self.myShaadiTableview.bounds.size.width, CGFloat.min)) 
       return headerView
    }
    else
    { 
       // Construct your other headers here 
    }
    

答案 19 :(得分:9)

我的答案将是更一般的答案,但也可以应用于此。

如果根视图的根视图 ViewController )或第一个子视图(子视图)是UIScrollView的子类(或UIScrollView本身),如果

self.navigationController.navigationBar.translucent = YES;

框架会自动设置预先计算的contentInset

为避免这种情况,您可以

self.automaticallyAdjustsScrollViewInsets = NO;

但是在我的情况下我无法做到这一点,因为我正在实现具有UIView组件的SDK,可供其他开发人员使用。该UIView组件包含UIWebView(其中UIScrollView作为第一个子视图)。如果将该组件添加为UIViewController视图层次结构中的第一个子组件,则系统将应用自动插入。

我已经通过在添加UIWebView之前添加虚拟视图与框架(0,0,0,0)来修复此问题。

在这种情况下,系统没有找到UIScrollView的子类作为第一个子视图,并且没有应用insets

答案 20 :(得分:8)

我认为这只是新UITableViewStyleGrouped样式的一部分。它位于所有分组表视图中,似乎没有任何直接的方法来控制该空间。

如果该空格由UIView表示,则可以搜索subviews的所有UITableView来查找该特定视图并直接对其进行编辑。但是,在标题和单元格开始之前,该空间也可能只是一个硬编码的偏移量,并且没有任何方法可以对其进行编辑。

要搜索所有子视图(我会在表没有单元格时运行此代码,以使其更容易阅读输出):

- (void)listSubviewsOfView:(UIView *)view {

    // Get the subviews of the view
    NSArray *subviews = [view subviews];

    // Return if there are no subviews
    if ([subviews count] == 0) return;

    for (UIView *subview in subviews) {

        NSLog(@"%@", subview);

        // List the subviews of subview
        [self listSubviewsOfView:subview];
    }
}

答案 21 :(得分:8)

这是通过Storyboard在iOS 11和Xcode 9.1中轻松修复的方法:

选择表格视图&gt;尺寸检验员&gt;内容插入:从不

答案 22 :(得分:7)

我和arielyz有同样的解决方法。一旦我将UITableView移动到不是父视图的第一个子视图,它就消失了。我的空间是20像素,而不是35像素。

我无法在肖像xib中重新创建它,只能使用横向xib。如果我可以在一个简单的演示应用程序中重现它,我将在稍后提交雷达错误。

答案 23 :(得分:7)

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

当我将0改为0.1f时,问题就消失了。

答案 24 :(得分:6)

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

 //  OR

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

   //OR

 self.automaticallyAdjustsScrollViewInsets = false
        }

答案 25 :(得分:6)

具体来说,要从顶部删除tableviewHeader空间,我做了以下更改:

YouStoryboard.storyboard&gt; YouViewController&gt;选择TableView&gt;尺寸检验员&gt;内容插入 - 将其设置为never。

enter image description here

答案 26 :(得分:6)

使用这个我认为这有帮助...

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 {
    return 0.005f;// set this according to that you want...
 }

答案 27 :(得分:6)

唯一对我有用的是:

<强>夫特

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

<强>目标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))

答案 28 :(得分:5)

我们有多个答案。

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

答案 29 :(得分:5)

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return CGFLOAT_MIN;
}

那是所有人!

答案 30 :(得分:4)

只需在视图的绝对顶部固定您的tableview(或使其开始)。额外的不需要的空间正好是导航栏的高度。

答案 31 :(得分:4)

我也一直在反对这一个。很确定这是一个iOS7错误。 最终帮助我的是xib中的视图顺序。我有一个视图正确显示表视图,另一个视图具有额外的35px空间。那时(UITableView明智)的唯一区别是,在显示不良的视图中,UITableView是第一个子节点,而在正确显示的视图中,它是第二个。

这对我有用,只是改变了视图的顺序。我真的不想为解决方法添加额外的代码行......

答案 32 :(得分:3)

tableView.contentInsetAdjustmentBehavior = .never

在带标题的纯表格视图中是我的解决方案(iOS12 / xcode 10)

答案 33 :(得分:3)

在下面(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
}

就是这样。模型对变化一无所知,因为我们在访问数据时会转换节号。

答案 34 :(得分:3)

我刚从UITableView 删除 Interface Builder再次创建,奇怪的35px消失了。

Interface Builder似乎有一个奇怪的错误。

答案 35 :(得分:3)

如果选择UITableViewStyleGrouped的样式,则需要实现HeaderFooter height委托方法,并且返回值需要大于0;像这样:

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

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

答案 36 :(得分:2)

当你需要隐藏tableHeaderView时,你应该使用这个Swift 3代码:

tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0.0, height: CGFloat.leastNormalMagnitude)))

答案 37 :(得分:2)

self.automaticallyAdjustsScrollViewInsets = NO;

此功能在iOS 11中已弃用,因此您应该使用新属性 contentInsetAdjustmentBehavior在您的代码中,它应该可以解决问题。

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

iOS 11中添加了UIScrollView上的新属性contentInsetAdjustmentBehavior,以确定调整内容偏移量

答案 38 :(得分:2)

当您使用UITableView.Style.grouped或UITableView.Style.InsetGrouped时,如果没有“ tableview标头”,则tableview将自动在“ section标头”中添加顶部插入填充,此修复很简单:

 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

通常就足够了

答案 39 :(得分:2)

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

答案 40 :(得分:2)

2021 Xcode 12.3 更新

要解决这个问题,需要禁用标准节页脚。 以下代码段完成了这项工作:

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return nil
}

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

使用

tableView.contentInsetAdjustmentBehavior = .never

这里完全无关

答案 41 :(得分:2)

使用故事板:

确保您的UITableView顶部约束不会显示“在顶部布局指南下”。相反,它应该说“Topview to:Superview”(Superview.Top)。

当然,在包含UITableView的UIViewController中,取消选中“Adjust Scroll View Insets”。

答案 42 :(得分:2)

我有一个带有Container的VC(btw是Nav控制器的一部分),并且嵌入了一个TableVC。

选择我的TableVC,查看属性面板:

  • 取消选中自动调整滚动插图
  • 取消选中顶栏下的延伸边

然后我将Container的autoconstraint设置为相对于父视图:

Container View.top = ParentView.Top Margin

这为我完成了以下任务:

  • 将桌子拉到顶部,但就在我的导航栏下方
  • 刷新后,表格返回到此位置,而不是正常情况下导航栏下方的<+
  • 滚动桌子底部的实际底部,而不是它

使用此设置对我进行了大量的反复试验,但我最终发现了lekksi @ Remove empty space before cells in UITableView发布的这些小细节

答案 43 :(得分:1)

在我的情况下,在Storyboard中的原型可视化中有一个名为“HeaderView”的不需要的单元格,当我删除它时一切都很好。

答案 44 :(得分:1)

我注意到这个问题有很多答案取决于你想要做什么,所以我会分享我的,以防任何人想要得到同样的效果:

enter image description here

在我的视图控制器中,我有一个分组UITableView,其tableHeaderView由200个高UIScrollView组成,而一个归因UILabel包含广告标题,价格,位置和发布的时间。下面是表格视图的内容。

使用分组表格视图标题的默认尺寸,“MORE INFO”标题远低于“5天前”标签。我通过覆盖每个部分的页眉和页脚的高度来修复它(已经是上面的图像)。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section == kMoreInfoSection ? 33 : UITableViewAutomaticDimension;
}

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

我从已接受的答案中了解了@ erurainon评论的高度,以及https://stackoverflow.com/a/14404104/855680UITableViewAutomaticDimension。我不需要在我的视图控制器中将self.automaticallyAdjustsScrollViewInsets设置为NO,这与接受的答案所暗示的不同。

我还尝试设置UIEdgeInsets并为表格视图提供顶部的负值,但它不起作用 - 整个表格视图向上移动,包括tableHeaderView。< / p>

答案 45 :(得分:1)

我有这个确切的问题,但是在tableView单元格内有一个tableView。 上面的无数答案无济于事。 一个非常奇怪而简单的事情起作用了:

-在为tableView分配委托之前放置估计的高度!

    override func awakeFromNib() {
        super.awakeFromNib()

        tableView.estimatedRowHeight = 58
        tableView.estimatedSectionHeaderHeight = 45
        tableView.estimatedSectionFooterHeight = 10
        tableView.dataSource = self
        tableView.delegate = self
    }

在委托分配之后放入估算值,导致我的内部tableView在标头上方有一个奇怪的空白。 希望对您有所帮助。

答案 46 :(得分:1)

我不确定这个好的答案是否适用于我

我选择表视图,导航到标尺,将Y值更改为零

enter image description here

答案 47 :(得分:1)

2019年答案:

您只需这样做

tableView.contentInsetAdjustmentBehavior = .never

Bizarre微妙的葛奇亚->

这些天来,Tableview的行为非常奇怪:

  1. 在具有槽口(例如XR等)的设备上,它会 不告诉您 而添加更多插图 但仅当时> 该表从屏幕的物理顶部开始。

  2. 如果在屏幕顶部开始 NOT ,则 不会 那个,但是

  3. 这两个案件与safeAreaInsets .......都不相关>>

所有这些都是完全没有记录的 ...您可能会浪费大量时间来弄清这一点。

如果您确实需要从屏幕/表格顶部开始进行测量,

实际上只是去:

tableView.contentInsetAdjustmentBehavior = .never

一个很好的例子显然是当您在表格的顶部添加某种横幅或类似的东西时(这在当今很普遍),并且您只需将表格的顶部插入设置为横幅/等在以下情况时变为的高度它正在运行。

为此,您必须使用

tableView.contentInsetAdjustmentBehavior = .never

致电:/

奖励哥

别忘了这些天几乎总是动态地加载一些信息(用户图片,描述等),因此在信息到达之前,您不能将这些值设置为最终所需的值。另一个gotchya。 :/

因此,您将拥有类似以下的代码:

func setTableOffsetOnceFlagAreaSizeIsKnown() {
    tableView.contentInset.top = yourSpecialFlagViewUpTop.bounds.height
}

override func viewDidLayoutSubviews() {
    setTableOffsetOnceFlagAreaSizeIsKnown()
}

答案 48 :(得分:1)

确保您已为表格提供约束。我没有并且遇到过类似的问题,即桌子底部有一个无法解释的填充物。

答案 49 :(得分:1)

我经历了所有的答案。没有人为我工作。我所要做的就是:

self.myTableView.rowHeight = UITableViewAutomaticDimension
self.myTableView.estimatedRowHeight = 44.0

此外,问题没有发生在tableView的顶部。它发生在tableView的每个部分的顶部。

FWIW这个问题只发生在iOS9上。我们的应用程序适用于iOS10和iOS 11。

我强烈建议你看看这个很棒的问题及其最佳答案:

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

答案 50 :(得分:0)

如果我添加一个高度非常小的tableHeaderView,它将解决此问题

    let v = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.000000001))
    tableViewDocuments.tableHeaderView = v

答案 51 :(得分:0)

以前的答案都不适合我。最终为我的情况工作的是什么(在向我的Table View动态添加标题后看到填充)是从Navigation Bar选择Document Outline并重新检查Translucent盒子(与一些说法迫使它变成半透明的答案相反)。我之前没有选中它,因为它使我的红色看起来有点橙色,但我无法尝试重新排序场景中元素的解决方案,因为Table View是此场景中唯一的元素。只要您可以使用Navigation Bar的颜色,并且在其上面有半透明层,此解决方案就可以正常工作。希望这会有所帮助:)

答案 52 :(得分:0)

对于第一部分,我们需要像这样设置tableHeaderView

tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.01))

以及其他部分,我们应该像这样设置heightForFooterInSection

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

答案 53 :(得分:0)

在iOS 7中,您可以查看屏幕的内容,而不是在navBar下方。它很容易设置到你导航栏的底部。检查解决方案的THIS答案,它不是同一个问题,但与您的问题非常相关。

答案 54 :(得分:0)

另一种做法...但我喜欢它,因为它可以避免硬编码任何高度值

UITableViewStyleGrouped表格(静态或动态)中,只需在tableView(_:heightForHeaderInSection)中指定所需的高度即可。我正在根据节标题标签的底部填充计算新的高度。

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    var height = UITableViewAutomaticDimension

    if section == 0, let header = tableView.headerViewForSection(section) {
        if let label = header.textLabel {
            // get padding below label
            let bottomPadding = header.frame.height - label.frame.origin.y - label.frame.height
            // use it as top padding
            height = label.frame.height + (2 * bottomPadding)
        }
    }

    return height
}

在iOS 9,Xcode 7.3上测试。

希望它有所帮助。

答案 55 :(得分:0)

我已经遇到了额外的顶级空间问题,我不得不提出很多答案才能找到解决方案。 (检查所有给出的答案需要5个小时)

首先如果您所关注的

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

无需对页眉或页脚部分进行任何更改,也无需添加与之相关的其他委托方法

然后在ViewController中的InterfaceBuilder中拥有TableView -取消选中调整滚动视图插图 -取消选中“延伸边缘:顶部栏下方”

*此外,请确保您要删除派生数据并在模拟器或手机中重新安装您的应用程序,以反映有效完成的更改。

由于IDE的原因,UI更改有时也无法反映出来... enter image description here

答案 56 :(得分:0)

仅取消选中Adjust Scroll View Insets对我不起作用。
后来,我尝试将tableview的标头设置为nil,这很幸运。

self.tableView.tableHeaderView = nil

答案 57 :(得分:0)

请检查表视图是否已分组/普通。就我而言,我已将样式更改为简单样式并且有效。现在页眉上方的多余空格已消失。

答案 58 :(得分:0)

最后想到了在尝试所有事情后如何解决这个问题!

我注意到由于某种原因,我的tableview的contentOffset的'y'为-20所以在配置我的tableview时: 迅速3/4

tableView.contentOffset = .zero

目标-C

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

答案 59 :(得分:0)

根据Apple documentation

  

将视图分配给此属性时,将该视图的高度设置为   非零值。表格视图仅考虑您的身高   视图的框架矩形;它调整标题视图的宽度   自动匹配表格视图的宽度。

快速5

如果您将UITableView分组为tableHeaderView

在将视图分配给tableView.tableHeaderView之前,将该视图的高度设置为非零值,例如:

// Set the initial height of your custom view to the smallest value  
myTableHeaderView.frame.size.height = .leastNonzeroMagnitude

// Assign your custom view to the header view
tableView.tableHeaderView = myTableHeaderView

// Set width constraint, height should grow based on your view. 
tableView.tableHeaderView!.widthAnchor.constraint(equalTo: tableView.widthAnchor).isActive = true

无需使用contentInsets进行魔术操作或处理委托方法。

答案 60 :(得分:0)

使用swift 4.1。试试这个:

func tableView(_ tableView: UITableView, viewForHeaderInSection 
section: Int) -> UIView? {
    return nil 
}

答案 61 :(得分:0)

set
  override func viewDidLoad() {
  self.tableView.delegate = self
  self.tableView.dataSource = self
}

答案 62 :(得分:0)

答案或解决方法都没有帮助我。大多数人都在处理tableView本身。对我造成问题的是我将tableView或collectionView添加到的视图。这就是为我解决的问题:

SWIFT 5

edgesForExtendedLayout = []

我把它放在了viewDidLoad中,烦人的差距消失了。希望这会有所帮助。

答案 63 :(得分:0)

快速4个答案...

如果在界面构建器中选择了表视图,并且在属性检查器中选择了“分组”样式,请在视图控制器中输入以下代码以解决额外的标题空间问题。

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

答案 64 :(得分:0)

如果self.automaticallyAdjustsScrollViewInsets = NO;对您不起作用,请确保表视图标题或节标题的高度为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;

希望有帮助。

答案 65 :(得分:0)

这段代码对我有用,上面写在objective-C上的对我来说是最好的答案,所以我将其转换为Swift。

对于Swift 4.0 +

self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: 0.01))

只需将其写入viewDidLoad()中,它将像冠军一样工作。

答案 66 :(得分:0)

对我来说,我尝试了所有解决方案,但我正在考虑为UITableView部分设置高度,并且对我有用。(使用 swift

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

答案 67 :(得分:0)

Swift3

  If you are using grouped table then do following things .
  This will remove top space.
    var frame = CGRect.zero
    frame.size.height = .leastNormalMagnitude
    self.tableView.tableHeaderView = UIView(frame: frame)
    self.tableView.tableFooterView = UIView(frame: frame)
If you Still getting this issue then it will remove by default content offset from top.
 [Set content insets never]          

https://i.stack.imgur.com/fjxOV.png

答案 68 :(得分:0)

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

这对我有用。也可以根据部分给出标题的高度。

答案 69 :(得分:-3)

投入我的十美分 - 从桌面视图中删除标题单元后,我遇到了同样的问题。为了解决这个问题,我将Attributes Inspector中的TableView Style 从“Grouped”更改为“Plain”,并删除了额外的空格。