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.
You can change the space between tableviewcells themselves by: [myTableView setSectionHeaderHeight:0]; [myTableView setSectionFooterHeight:25];
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).
Style. grouped. A table view where sections have distinct groups of rows.
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.
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)
As others have noted you can use this same solution for footers.
See the Documentation for more details on the tableHeaderView
property.
Thanks to @liushuaikobe for verifying using the least positive normal number works.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With