通过View Debug调试自定义UICollectionView时,它的宽度为829
,但是当通过代码访问它的宽度时,它的宽度为820
。我错过了什么?
可视化调试:
代码:
func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize
{
let widthForCulculation = self.frame.size.width // 820.0
...
return CGSize(width: segmentWidth, height: segmentHeight)
}
答案 0 :(得分:1)
非常hacky方式,但在布局子视图时尝试使布局无效
var width : CGFloat = 0
override func layoutSubviews() {
super.layoutSubviews()
if (self.width != self.frame.size.width)
{
self.width = self.frame.size.width
self.collectionViewLayout.invalidateLayout()
}
}