我正在尝试使用
UICollectionView
显示摄像头流来创建 tvOS 项目。
在
UICollectionViewCell
中,我使用 TVPosterView
显示流/图像和标题。
我目前面临的问题是,当应用程序启动时,所有缩略图/
UICollectionViewCell
似乎都有合适的大小;第一个项目的尺寸比其他项目更大。当我推出到其他项目时,先前聚焦的项目变得比开始时的大小小得多。
如果我没说清楚,请让我尝试解释一下情况。
当应用程序启动时 -
Camera A
比其他应用程序集中且尺寸更大:
展开
Camera A
时,它变得比Camera D
或Camera E
小很多;另外,当前聚焦的 Camera B
不再具有更大的尺寸(如果我与上一个屏幕中的 Camera A
尺寸进行比较):
在
UICollectionView
控制器中,我有以下委托方法:
extension ListingViewController: UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let thumbWidth = CGFloat(320)
let thumbHeight = CGFloat(thumbWidth * 0.7)
return CGSize(width: thumbWidth, height: thumbHeight)
}
}
在
UICollectionViewCell
我基本上有以下代码:
internal func setupUI()
{
posterView = TVPosterView()
posterView.tag = 1100
posterView.clipsToBounds = false
addSubview(posterView)
posterView.alignToSuperView()
}
alignToSuperView() 基本上是
UIView
的扩展,代码如下:
public func alignToSuperView()
{
self.translatesAutoresizingMaskIntoConstraints = false
guard let margins = self.superview?.layoutMarginsGuide else {
return
}
self.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
self.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
self.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
self.superview?.layoutIfNeeded()
}
老问题,但如果其他人遇到这个问题,我可以在配置新数据模型后通过在单元上调用
setNeedsLayout()
和 layoutIfNeeded()
来解决它。