How to remove header and footer space from a UITab

2020-05-20 04:17发布

I have a UITableView in the grouped style, and only one section. However there is some blank space above and below the table view that is shown when the user scrolls too far. How can I remove this blank space?

9条回答
The star\"
2楼-- · 2020-05-20 05:03

you can also use this code for removing space between first cell of uitableview..

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 {
    return 0.002f;// set this...
 }
查看更多
姐就是有狂的资本
3楼-- · 2020-05-20 05:06

You can do this by altering the contentInset property that the table view inherits from UIScrollView.

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

This will make the top and bottom touch the edge.

查看更多
Root(大扎)
4楼-- · 2020-05-20 05:06

Add this code:

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
查看更多
Anthone
5楼-- · 2020-05-20 05:07

Uncheck Extend Edges Under Top bar.

enter image description here

查看更多
萌系小妹纸
6楼-- · 2020-05-20 05:12

UIView can be inserted at the top and bottom of the table(drag and drop). Set their properties as transparent and height of 1 px. This is to remove the extra padding in front of the cells.

查看更多
一夜七次
7楼-- · 2020-05-20 05:18

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

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.

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

Swift:

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.


Sources and Acknowledgements

See the Documentation for more details on the tableHeaderView property.

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

My original answer: https://stackoverflow.com/a/22185534/2789144

查看更多
登录 后发表回答