Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

objective c - How to remove the blank space at the top of a grouped UITableView?

When you create a UITableView with the UITableViewStyleGrouped style, it adds quite a lot of space in between the actual tableviewcells and the borders of the table's frame. This space is above, below, to the left, and to the right of the tableviewcells.

You can change the space between tableviewcells themselves by:

[myTableView setSectionHeaderHeight:0];
[myTableView setSectionFooterHeight:25];

However, even by doing that, there's still this annoying space between the top of the frame and the first tableviewcell. Furthermore, there's a ton of space still in between the left of the frame and the tableviewcell, as well as the right side of the frame and the tableviewcell.

-=-=-=-=-=-=-

Is there a way to manipulate that space (resize it)? My only solution thus far is to make the size of the frame larger than the screen to fake out the program into having that "blank space" outside of the screen, thus removing it. However, this is obviously not optimal and a clear hack.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This answer comes quite late, but I hope it helps someone.

The space is there because of the UITableView's tableHeaderView property. When the the tableHeaderView property is nil Apple defaults a view. So the way around this is to create an empty view with a height greater than 0. Setting this overrides the default view thereby removing the unwanted space.

This can be done in a Storyboard by dragging a view to the top of a tableView and then setting the height of the view to a value of 1 or greater.

Or it can be done programmatically with the following code:

Objective-C:

CGRect frame = CGRectZero;
frame.size.height = CGFLOAT_MIN;
[self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:frame]];

Swift:

var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tableView.tableHeaderView = UIView(frame: frame)

Comments

As others have noted you can use this same solution for footers.


Sources and Acknowledgements

See the Documentation for more details on the tableHeaderView property.

Thanks to @liushuaikobe for verifying using the least positive normal number works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...