Grouped UITableView has 20px of extra padding at the bottom

Objective CIosUitableview

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

@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;
} 

Solution 2 - Objective C

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

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

Solution 3 - Objective C

in Swift 3

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

Solution 4 - Objective C

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

It's work for me

Solution 5 - Objective C

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

Solution 6 - Objective C

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))

Solution 7 - Objective C

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
}

Solution 8 - Objective C

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)

Solution 9 - Objective C

Following code works for me

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

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

Solution 10 - Objective C

Swift 3 code

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

Solution 11 - Objective C

Swift 5

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

Solution 12 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVlad PolyanskiyView Question on Stackoverflow
Solution 1 - Objective CtounaobunView Answer on Stackoverflow
Solution 2 - Objective Can0View Answer on Stackoverflow
Solution 3 - Objective CzippoView Answer on Stackoverflow
Solution 4 - Objective CfrankWhiteView Answer on Stackoverflow
Solution 5 - Objective CabhimuralidharanView Answer on Stackoverflow
Solution 6 - Objective CDan LeeView Answer on Stackoverflow
Solution 7 - Objective CJoeView Answer on Stackoverflow
Solution 8 - Objective CYashView Answer on Stackoverflow
Solution 9 - Objective CAmit BattanView Answer on Stackoverflow
Solution 10 - Objective ChhammView Answer on Stackoverflow
Solution 11 - Objective CDenView Answer on Stackoverflow
Solution 12 - Objective CKarenAnneView Answer on Stackoverflow