Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 318
Highrule Avatar asked Mar 29 '12 20:03

Highrule


People also ask

How do you remove the blank space at the top of a grouped UITableView?

You can change the space between tableviewcells themselves by: [myTableView setSectionHeaderHeight:0]; [myTableView setSectionFooterHeight:25];

Why is there extra padding at the top of my UITableView?

Starting in iOS7, there is additional space at the top of my UITableView 's which have a style UITableViewStyleGrouped . The tableview starts at the first arrow, there are 35 pixels of unexplained padding, then the green header is a UIView returned by viewForHeaderInSection (where the section is 0).

What is grouped UITableView?

Style. grouped. A table view where sections have distinct groups of rows.

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.


2 Answers

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.

like image 138
James Nelson Avatar answered Sep 20 '22 02:09

James Nelson


Answer in Swift 4

If the table view is selected in interface builder and in the attributes inspector the style "Grouped" is selected, enter the following code in your view controller to fix the extra header space issue.

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {     return CGFloat.leastNonzeroMagnitude } 
like image 35
F. Morales Avatar answered Sep 21 '22 02:09

F. Morales