我为iOS 8制作了一个应用程序,它使用分组的UITableView
作为其页面之一。其中有多个部分使用CGFloat.leastNormalMagnitude
(或Swift 2及更低版本中的CGFloat.min
)用于剖面页眉和页脚高度以删除“默认”空间。一切顺利,直到应用程序在iOS 9和10中运行,它崩溃时出现此错误:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'section header height不能为负 - 提供的第0部分的高度为-0.00000'
不知何故,1
下的任何值(除了舍入的0
)都被视为否定 - 并且使用1
作为返回值将使页眉/页脚空间再次出现。
有没有解决方法来解决这个问题?
提前致谢。
我为tableView(_:heightForHeaderInSection:)
尝试了几个值,并发现:
leastNormalMagnitude
和leastNonzeroMagnitude
将被视为减号(因此崩溃)。我最终使用1.1
来解决我的问题。
希望这能帮助那里的人!
我们使用运行Xcode 10.2的Swift 5在iOS 9上经历了相同的运行。事实证明,如果您在estimatedHeightForHeaderInSection / estimatedHeightForFooterInSection中返回CGFloat.leastNormalMagnitude或CGFloat.leastNonzeroMagnitude,它将在iOS 9设备上崩溃。
您仍然可以在heightForHeaderInSection / heightForFooterInSection中返回leastNormalMagnitude或leastNonzeroMagnitude。
正如edopelawi在上面指出的那样,任何小于1的值都将被视为负数,1将被视为默认的分组部分页眉/页脚高度。如果设备运行iOS 10或更低版本,我们最终返回1.000001作为估计高度:
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
if #available(iOS 11.0, *) {
return self.tableView(tableView, heightForHeaderInSection: section)
} else {
return max(1.000001, self.tableView(tableView, heightForHeaderInSection: section))
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 10
}
return CGFloat.leastNonzeroMagnitude
}
如果我设置节标题高度,我有同样的问题:
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude
但是如果我将我的Controller设置为我的表视图的委托(UITableViewDelegate)并实现:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
然后它工作
也许CGFloat.leastNonzeroMagnitude
是你需要的!