iOS 11 适配(一)UITableViewStyleGrouped 类型 tableView sectionHeader 高度问题

适配 iOS 11 是最近工作中的重中之重,发现的问题也有很多。

UITableViewStyleGrouped 类型的 tableView 在适配的时候出现很大的问题。记录一下

按照之前的方法,只需要执行以下的代码就能够很好的解决 section == 0 的时候,sectionHeader 的高度问题以及 section 间距的问题

tableView.delegate = self; tableView.dataSource = self; tableView.tableFooterView = [UIView new];

配合这两种方法

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

但是,在 iOS 11 上通过这两种方法已经不能解决问题,通过大量的测试,始终发现,在 iOS 10 以下版本中都能够解决问题,但是在 iOS11 上不能够解决问题,经过研究之后发现,通过以下的方法能够良好的解决 sectionHeader 的高度问题,并且是兼容 iOS 10 以及其他版本的

tableView.delegate = self; tableView.dataSource = self; tableView.sectionFooterHeight = 0.01f; tableView.tableFooterView = [UIView new];

  1. 首先,在实例化tableView 的时候,直接声明 sectionFooterHeight,不需要写代理方法
  2. 声明 sectionHeaderView 高度的时候,不能够再像以前一样仅仅声明高度,而是,直接使用比较粗暴的方式,声明一个 view 出来 ,这样就能像从前一样实现效果

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, 10.0f)]; return headerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 10.0f; }

两种方法必须同时写,不然还是不生效,不知道因为什么,从苹果的官方文档还是没有获取到关于footerView 的更多解释,猜测可能还是在 runtime 的时候,苹果做了不知道的事情

亲测是这样的效果,如果大神能够有更好的解决方式,希望提供,欢迎拍砖

更新: 昨天晚上比较匆忙,忘记测试了两项比较重要的属性,今天更新一下,发现只要设置以下属性,能够良好的解决问题

tableView.estimatedSectionHeaderHeight = 0; tableView.estimatedSectionFooterHeight = 0;

设置 tableView 的sectionHeader、FooterView 的高度之后,就就可以解决高度不准确的问题 在系统更新之后,tableView 的 section 的头尾预估高度不再是0,官方文档解释如下

// default is UITableViewAutomaticDimension, set to 0 to disable

感谢各位大神

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值