Why is there extra padding at the top of my UITabl

2018-12-31 17:09发布

Starting in iOS7, there is additional space at the top of my UITableView's which have a 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 UIView returned by viewForHeaderInSection (where the section is 0).

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

30条回答
唯独是你
2楼-- · 2018-12-31 17:39

The only thing that worked for me was:

Swift:

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

Objective-C:

self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 0;

Also, I still had an extra space for the first section. That was because I was using the tableHeaderView property incorrectly. Fixed that as well by adding:

self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 0.01))
查看更多
人间绝色
3楼-- · 2018-12-31 17:40

Try changing the contentInset property that UITableView inherits from UIScrollView.

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

It's a workaround, but it works

查看更多
若你有天会懂
4楼-- · 2018-12-31 17:41

Another quick comment... even in XCode 6.1, there is a bug with vertical spaces appearing at the top of UIScrollViews, UITextViews and 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 first subview on the page.

enter image description here

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

查看更多
残风、尘缘若梦
5楼-- · 2018-12-31 17:41

My answer is going to be more general answer, but can be applied on this as well.

If the root view (of the ViewController) or the first child (subview) of the root view is subclass of the UIScrollView (or UIScrollView itself), and if

self.navigationController.navigationBar.translucent = YES;

framework will automatically set pre-calculated contentInset.


To avoid this you can do

self.automaticallyAdjustsScrollViewInsets = NO;

but in my case I wasn't able to do this, because I was implementing SDK which has UIView component which can be used by other developers. That UIView component contains UIWebView (which has UIScrollView as the first subview). If that component is added as the first child in the UIViewController's view hierarchy, automatic insets will be applied by system.

I've fixed this by adding dummy view with frame (0,0,0,0) before adding UIWebView.

In this case system didn't find subclass of the UIScrollView as the first subview and didn't apply insets

查看更多
一个人的天荒地老
6楼-- · 2018-12-31 17:42

I had the same fix as arielyz. Once I moved the UITableView to be not the first subview of the parent view, it went away. My space was 20 px, not 35.

I wasn't able to recreate it in a portrait xib, only a landscape xib. I'll file a radar bug later if I can reproduce it in a simple demo app.

查看更多
浮光初槿花落
7楼-- · 2018-12-31 17:43
self.automaticallyAdjustsScrollViewInsets = NO;

try, you can deal with it!

查看更多
登录 后发表回答