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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18880341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:20:45  来源:igfitidea点击:

Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7

iosuitableviewios7

提问by esilver

Starting in iOS7, there is additional space at the top of my UITableView's which have a style UITableViewStyleGrouped.

从 iOS7 开始, my 顶部有额外的空间,UITableView其中有一个 style UITableViewStyleGrouped

Here is an example:

下面是一个例子:

enter image description here

在此处输入图片说明

The tableview starts at the first arrow, there is 35 pixels of unexplained padding, then the green header is a UIViewreturned by viewForHeaderInSection(where the section is 0).

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

Can anyone explain where this 35 pixel amount is coming from and how I can get rid of it without switching to UITableViewStylePlain?

谁能解释这个 35 像素的数量来自哪里,以及我如何在不切换到的情况下摆脱它UITableViewStylePlain



Note:

笔记:

In iOS 11 and later:

在 iOS 11 及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

采纳答案by esilver

I have found the cause of my original bug and created a sample project showcasing it. I believe there is an iOS7 bug.

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

As of iOS7, if you create a UITableView with the Grouped style, but do not have a delegate set on first layout, then you set a delegate and call reloadData, there will be a 35px space at the top that will never go away.

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

See this project I made showcasing the bug: https://github.com/esilverberg/TableViewDelayedDelegateBug

请参阅我展示错误的这个项目:https: //github.com/esilverberg/TableViewDelayedDelegateBug

Specifically this file: https://github.com/esilverberg/TableViewDelayedDelegateBug/blob/master/TableViewDelayedDelegateBug/ViewController.m

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

If line 24 is active,

如果第 24 行处于活动状态,

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

there will be an extra 35 px space at the top. If line 27 is active and 24 is commented out,

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

self.tableView.delegate = self;

no space at the top. It's like the tableView is caching a result somewhere and not redrawing itself after the delegate is set and reloadData is called.

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

回答by Alexander

I was helped by the following:

我得到了以下帮助:

YouStoryboard.storyboard > YouViewController > Attributes inspector > Uncheck - Adjust scroll view insets.

YouStoryboard.storyboard > YouViewController > Attributes inspector > Uncheck - Adjust scroll view insets。

enter image description here

在此处输入图片说明

回答by Mr. T

I played around with it a bit more and it seems like this is a side-effect of setting the tableView's tableHeaderView = nil.

我玩了更多,似乎这是设置 tableView 的副作用tableHeaderView = nil

Because my tableView has a dynamically appearing tableHeaderView, when I need to hide the tableHeaderView, instead of doing self.tableView.tableHeaderView = nil;, I do:

因为我的 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)];

I like this solution better than setting a somewhat arbitrary contentInset.topbecause I use the contentInset.topdynamically as well. Having to remember to remove an extra 35px whenever I recalculate contentInset.topis tedious.

我更喜欢这个解决方案,而不是设置一个有点随意的,contentInset.top因为我也使用contentInset.top动态的。每次重新计算时contentInset.top都必须记住删除额外的 35px很乏味。

回答by yeahdixon

For IOS 7 if you are allocing a tableview in a view controller you may look into

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

self.edgesForExtendedLayout = UIRectEdgeNone;

your problem seemed similar to mine

你的问题似乎和我的相似

Update:

更新:

Swift in iOS 9.x:

iOS 9.x 中的 Swift:

self.edgesForExtendedLayout = UIRectEdge.None

Swift 3 :

斯威夫特3:

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

回答by nvrtd frst

Try changing the contentInsetproperty that UITableViewinherits from UIScrollView.

尝试更改从 继承的contentInset属性。UITableViewUIScrollView

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

It's a workaround, but it works

这是一种解决方法,但它有效

回答by guanhuiwit

self.automaticallyAdjustsScrollViewInsets = NO;

try, you can deal with it!

试试吧,你可以解决的!

回答by LightMan

You could detect if your app is running iOS7 or greater and add this two methods in your table view delegate (usually in your UIViewController code)

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

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

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

This maybe is not an elegant solution but works for me

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

Swift version:

迅捷版:

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

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

回答by Tú ?t

Uncheck "Adjust Scroll View insets"

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

enter image description here

在此处输入图片说明

回答by Mike Gledhill

Another quick comment... even in XCode 6.1, there is a bug with vertical spaces appearing at the top of UIScrollViews, UITextViewsand UITableViews.

另一个快速评论...即使在 XCode 6.1 中,也有一个错误,在UIScrollViews,UITextViews和的顶部出现垂直空格UITableViews

enter image description here

在此处输入图片说明

Sometimes, the only way to fix this issue is to go into the Storyboard and drag the problem control so it's no longer the firstsubview on the page.

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

enter image description here

在此处输入图片说明

(My thanks to Odedfor pointing me in this direction... I'm posting this comment, just to add a few screenshots, to demonstrate the symptoms and fix.)

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

回答by girish_vr

According to this transition guide for iOS7by Apple, the scroll view's content insets is automatically adjusted. The default value of automaticallyAdjustsScrollViewInsetsis set to YES.

根据Apple 的iOS7 过渡指南,滚动视图的内容插入会自动调整。AutomaticAdjustsScrollViewInsets的默认值设置为 YES。

The UIViewController which has the UITableView should set this property to NO.

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

self.automaticallyAdjustsScrollViewInsets = NO;

This will do the trick.

这将解决问题。

EDIT 1:

编辑 1:

Also, one could try -

另外,可以尝试 -

self.navigationController.navigationBar.translucent = YES;

This also removes the extra padding on the top.

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