[英]Collection View Cell subview frame not available until cell is reused
I have collection view cells that contain a subview of which I am trying to get the frame of in relation to the superview. 我有一个集合视图单元格,其中包含一个子视图,我正在尝试获取该子视图相对于超级视图的框架。
So far, I've managed to successfully get the frame of the subview, however for the first x amount of visible cells, the returned frame is always (0,0,0,0). 到目前为止,我已经成功地获取了子视图的框架,但是对于前x个可见单元格,返回的框架始终为(0,0,0,0)。
After I scroll them out of view and scroll back to those x cells, the returned frame is fine and the frames for the subviews are computed properly. 在将它们滚动到视图之外并滚动回到这x个单元格之后,返回的帧就很好了,并且正确计算了子视图的帧。
Any thoughts? 有什么想法吗?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let window = UIApplication.shared.keyWindow
let selectedCell = self.collectionView(self.cellCollectionView, cellForItemAt: indexPath) as! postCell
let photoItem = selectedCell.photo
let photoItemPosition = photoItem.frame
let convertedFrame = selectedCell.convert(photoItemPosition, to: window)
print(photoItemPosition)
print(convertedFrame)
}
You have to update the frame of photo
instance in layoutSubviews
. 您必须在layoutSubviews
更新photo
实例的框架。 Override the layoutSubviews
in your collectionView cell class 覆盖collectionView单元类中的layoutSubviews
override func layoutSubviews ()
{
super.layoutSubviews()
//Set the self.photo frame
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.