ChatGPT解决这个技术问题 Extra ChatGPT

分组的 UITableView 在底部有 20px 的额外填充

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.

Found the same issue here :(
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.

t
tounaobun

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

Good work building on @frankWhite's answer to make it less ambiguous.
In order to make it work use this func prototype: func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
bless your soul
return CGFloat.leastNormalMagnitude in swift 3.0 and higher
I also had to implement viewForFooterInSection returning nil to make it work in iOS 11+
a
an0

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

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

works for me. But this is sounding arbitrary. Why did I get space in the top for my grouped table starting ios5.0 in the first place?
@Gmu I don't know. Apple always arbitrarily adjusts UIKit to its own fit, which often leaves us 3rd-party devs fumbling :( iOS 7 is even worse in this aspect.
z
zippo

in Swift 3

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

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

It's work for me


Brilliant! Setting it to 0 has no effect. I even used 0.0001. I'm using Xcode 6.3 for iOS8.3 so this is working on latest release of Xcode.
I suggest going with @BensonTommy's answer as it is less ambiguous.
Thank you so much! What a terrible API the poor, sad UITableView has. Time to rewrite from the ground-up. But hopefully, without UICollectionViewController's rampant verbosity!
C
Community

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


D
Dan Lee

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

Good call to set tableFooterView, strange but it works. I already have tableView(_:viewForHeaderInSection:) implemented, so I tried adding the heightForFooter and viewForFooter section as described here and like other answers, those didn't fix it for me, but creating a dummy tableFooterView worked.
Y
Yash

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)

S
Sourabh Sharma

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
}

A
Amit Battan

Following code works for me

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

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

Thank you for your answer. But it doesnt work for me. I need to replace not section footer but table footer. Only one thing that can help is tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0,0,1,1)]; but it decrease padding to 1px not absolutelly remove it.
I have face similar issue in a grouped tableview for login & password. But solved using this. Have you try tableView.tableFooterView = nil;
Yes i try to setup tableFooterView to nil, but it doesnt work for me. Looks like compiler ignores it. I have many sections so i cant use your code. But also if i just create a clear project with only one groupped tableview it work in the same way ads extra padding at the bottom. Even settings app have the same padding.
Setting tableFooterView to nil worked for me! Thanks! Throw that in your answer and ill give it a +1
h
hhamm

Swift 3 code

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

D
Den

Swift 5

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

K
KarenAnne

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.


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅