如何删除分组的UITableView顶部的空白? [英] How to remove the blank space at the top of a grouped UITableView?

查看:376
本文介绍了如何删除分组的UITableView顶部的空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您创建具有UITableViewStyleGrouped样式的UITableView时,它将在实际的tableviewcell和表格框架的边框之间增加很多空间.该空间位于tableviewcell的上方,下方,左侧和右侧.

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.

您可以通过以下方式更改tableviewcell自身之间的空间:

You can change the space between tableviewcells themselves by:

[myTableView setSectionHeaderHeight:0];
[myTableView setSectionFooterHeight:25];

但是,即使这样做,在框架顶部和第一个tableviewcell之间仍然有这个烦人的空间.此外,在框架的左侧和tableviewcell之间以及框架的右侧和tableviewcell之间仍有大量空间.

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.

推荐答案

这个答案来得很晚,但我希望它能对某人有所帮助.

This answer comes quite late, but I hope it helps someone.

由于UITableViewtableHeaderView属性,因此存在空格.当tableHeaderView属性为nil时,Apple默认为视图.因此,解决此问题的方法是创建一个高度大于0的空视图.设置此设置会覆盖默认视图,从而删除不需要的空间.

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.

这可以在情节提要中完成,方法是将视图拖动到tableView的顶部,然后将视图的高度设置为1或更大的值.

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]];

快捷键:

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


评论

正如其他人所指出的,您可以将相同的解决方案用于页脚.


Comments

As others have noted you can use this same solution for footers.

请参见

See the Documentation for more details on the tableHeaderView property.

感谢@liushuaikobe使用最小正数进行验证.

Thanks to @liushuaikobe for verifying using the least positive normal number works.

这篇关于如何删除分组的UITableView顶部的空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆