57
votes

Grouped table views seem to have extra padding on the bottom in iOS 6 (iOS 5 does not have it), but I can't find any documentation that suggests this is correct / expected behavior.

This affects the example projects as well, for instance the SimpleTableView project in the TableViewSuite example. I think I had to change the style in the AppDelegate to 'grouped', and updated the SDK to iOS 6, but no other changes have been made to the project.

Investigating revealed that there are 10px reserved for header and footer views, plus some 20px that can't be accounted for. There are no actual header or footer views (tableHeaderView and tableFooterView are nil, and implementing and returning nil for e.g. viewForFooterInSection does nothing). I cannot find any '20' value on the tableView itself, though I may have missed something of course.

Adding a zero-size view for the footer does nothing, but adding a 1px square view causes the extra padding to vanish. e.g.:

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0,0,1,1)];

It does take up 1px of height still, so the bottom padding is now 11px, but this is far less noticeable than 20. And now setting the sectionFooterHeight to 0 will result in only 1px of bottom-space.

My question is: what? And how can I completely remove it? This isn't anything mission-critical, but it is extremely weird, undesirable, and as far as I can tell it's undocumented.

Please note - its copy past question from apple dev forum. But I have exactly the same issue and I don't understand how to solve it too.

12
Found the same issue here :(Barum Rho
If you are still using SO, you should award the answer to Frank White, as his solution works great in iOS8.3. Setting the footer value to zero has no effect.David H

12 Answers

83
votes

@frankWhite' solution works great, but here is a better solution to avoid typing 0.1, 0.001 or others to make it less ambiguous.

// Swift version
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    // remove bottom extra 20px space.
    return CGFloat.min
}
// Objective-C version
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    // remove bottom extra 20px space.
    return CGFLOAT_MIN;
} 
32
votes

You can use contentInset to compensate the 20px extra bottom margin:

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

in Swift 3

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}
11
votes
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   return 0.01;
} 

It's work for me

3
votes

This is the only working solution I guess.I tried setting tableFooterView and tableHeaderView to nil. But nothing happened.Then the following solution worked .Returning zero "return 0;" has no effect.We should return the lowest possible height.

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

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

NOTE: This is what I got when printed the values.

CGFLOAT_MAX = 340282346638528859811704183484516925440.000000

CGFLOAT_MIN = 0.000000

2
votes

Recently, I found this issue, and followed the solutions posted above, but all of them doesn't work. In my case, I have a section header with dynamic height. Inspired by the above solutions, I put following code, and problem solved. I hope this can help.

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView()
}

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

and config tableView's header and footer like this:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 0.1))
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 0.1))
1
votes

In Swift 4.2 you can try setting table header with least normal magnitude height -

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

Following code works for me

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

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

Swift 3 code

class PaddingLessTableView : UITableView
{
    override func layoutSubviews() {
        self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        super.layoutSubviews()
    }
}
0
votes

Swift 5

 tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: .leastNormalMagnitude))
0
votes

this solves the issue:

Obj-c

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

Swift:

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.1
}
-2
votes

Use the bounces property of UIScrollView:

[yourTableView setBounces:NO];

This will remove what seems to be an extra padding at the top and bottom of your UITableView. Actually, it will just disable the tableview's scrollview to scroll past the edge of the content.