View中的集合视图框架调试与代码不同

时间:2018-06-14 09:28:17

标签: ios swift debugging uicollectionview uicollectionviewlayout

通过View Debug调试自定义UICollectionView时,它的宽度为829,但是当通过代码访问它的宽度时,它的宽度为820。我错过了什么?

可视化调试:

enter image description here

代码:

func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize
    {
        let widthForCulculation = self.frame.size.width // 820.0
        ...
        return CGSize(width: segmentWidth, height: segmentHeight)
    }

1 个答案:

答案 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()
   }
}