我想在我的应用程序中支持iPhone X屏幕,现在工作还可以,但是我遇到了一个我看不到解决方案的障碍。我的UITableView的每个部分都有一个标题。我曾经像这样计算视图的高度:
override func sizeThatFits(_ size: CGSize) -> CGSize {
var height = CGFloat(0)
height += Metrics.lineTopHeight
height += Metrics.padding
height += headerLabel.sizeThatFits(size).height
height += Metrics.padding
height += Metrics.lineBottomHeight
return CGSize(width: size.width, height: height)
}
但是对于iPhone X,我已将水平限制改为:
if #available(iOS 11.0, *) {
constraints.append(headerLabel.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor))
constraints.append(headerLabel.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor))
} else {
constraints += DXVisual.vfl("H:|[headerLabel]|", views: views)
}
所以逻辑上我没有空间让我的headerLabel进入,所以我需要改变我的sizeThatFits函数。我认为它应该是这样的:
override func sizeThatFits(_ size: CGSize) -> CGSize {
let safeWidth: CGFloat
if #available(iOS 11.0, *) {
safeWidth = size.width - safeAreaInsets.left - safeAreaInsets.right
} else {
safeWidth = size.width
}
var height = CGFloat(0)
height += Metrics.lineTopHeight
height += Metrics.padding
height += headerLabel.sizeThatFits(CGSize(width: safeWidth, height: size.height)).height
height += Metrics.padding
height += Metrics.lineBottomHeight
return CGSize(width: size.width, height: height)
}
但还没有设置safeAreaInsets。我该怎么做或者我一起做错了;)
提前致谢