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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14210474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:45:45  来源:igfitidea点击:

Grouped UITableView has 20px of extra padding at the bottom

objective-ciosuitableview

提问by Vlad Polyanskiy

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.

分组表视图似乎在 iOS 6 的底部有额外的填充(iOS 5 没有),但我找不到任何表明这是正确/预期行为的文档。

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

这也会影响示例项目,例如示例中的SimpleTableView项目TableViewSuite。我想我必须将样式更改AppDelegate为“分组”,并将 SDK 更新到 iOS 6,但没有对项目进行其他更改。

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

调查显示,有10px保留用于页眉和页脚视图,还有一些20px无法解释的。没有实际的页眉或页脚视图(tableHeaderView并且tableFooterViewnil,并且实现和返回nil例如viewForFooterInSection什么都不做)。我在 tableView 本身上找不到任何“20”值,尽管我当然可能错过了一些东西。

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

为页脚添加一个零大小的视图没有任何作用,但添加一个1px方形视图会导致额外的填充消失。例如:

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

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

1px仍然占用高度,所以底部填充现在是11px,但这远没有 20 明显。现在将 设置sectionFooterHeight为 0 将只1px导致底部空间。

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.

请注意 - 它从苹果开发论坛复制过去的问题。但我有完全相同的问题,我也不明白如何解决它。

回答by 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.

@frankWhite 的解决方案效果很好,但这里有一个更好的解决方案,可以避免键入 0.1、0.001 或其他内容以减少歧义。

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

回答by an0

You can use contentInsetto compensate the 20px extra bottom margin:

您可以使用contentInset来补偿 20px 额外的底部边距:

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

回答by zippo

in Swift 3

在斯威夫特 3

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

回答by frankWhite

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

It's work for me

这对我有用

回答by abhimuralidharan

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

我猜这是唯一可行的解​​决方案。我尝试设置tableFooterView并设置tableHeaderViewnil. 但是什么也没发生。然后下面的解决方案起作用了。返回零“返回0;” 没有效果。我们应该返回尽可能低的高度。

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

CGFLOAT_MAX = 340282346638528859811704183484516925440.000000

CGFLOAT_MIN = 0.000000

回答by 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 的页眉和页脚:

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

回答by Den

Swift 5

斯威夫特 5

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

回答by 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;
}

回答by hhamm

Swift 3 code

斯威夫特 3 代码

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

回答by Joe

this solves the issue: Obj-c- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.1; }

这解决了这个问题: 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 }

Swift:覆盖 func tableView(_ tableView: UITableView, heightForFooterInSection 部分: Int) -> CGFloat { return 0.1 }