解决TableView类型为group顶部和底部留有空白问题

当UITableView的style为UITableViewStyleGrouped时,发现第一个cell和顶部留有一定的空白,tableView的底部也留有一定的空白,大小分别为35pt和20pt。

解决方法

  1. 想要控制首个cell距离顶部的距离,可以去控制tableView的偏移量来控制cell的高度:
self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, -20, 0)
  1. 上面这个方式tableView没有刷新功能时很简单实用,在tableView具有下拉和上拉刷新时,这样的偏移会使得刷新框架的图标也偏移了,可能被遮挡看不见,这个时候可以使用下面的方法:
self.tableView.estimatedSectionHeaderHeight = 0.01;
self.tableView.estimatedSectionFooterHeight = 0.01;

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

在iOS15系统上面,plain形式的section和cell之间会存在一个20的间隙,采用以下方式

if (@available(iOS 15.0, *)) {
     [self.tableView setSectionHeaderTopPadding:0.0f]; 
}

全局修改

if (@available(iOS 15.0, *)) {
    [[UITableView appearance] setSectionHeaderTopPadding:0.0f]; 
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容