UICollectionViewLayout 不在单元格内调整 UIImageViews 的大小
UICollectionViewLayout Not Resizing UIImageViews Within Cells
我的 collectionview 图片没有调整大小。我在包含集合视图的 viewcontroller 中使用以下内容,并且当单元格为空时它的大小正确;然而,它里面有一个 imageview 并不能正常工作。
谢谢!
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let noOfCellsInRow = 2
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let totalSpace = flowLayout.sectionInset.left
+ flowLayout.sectionInset.right
+ (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(noOfCellsInRow))
return CGSize(width: size, height: size)
}
将此添加到您的 ViewController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
并在 Storyboard
中检查集合视图 - Estimate Size
是否在 Size Inspector
中设置为 None
我的 collectionview 图片没有调整大小。我在包含集合视图的 viewcontroller 中使用以下内容,并且当单元格为空时它的大小正确;然而,它里面有一个 imageview 并不能正常工作。
谢谢!
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let noOfCellsInRow = 2
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let totalSpace = flowLayout.sectionInset.left
+ flowLayout.sectionInset.right
+ (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(noOfCellsInRow))
return CGSize(width: size, height: size)
}
将此添加到您的 ViewController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
并在 Storyboard
中检查集合视图 - Estimate Size
是否在 Size Inspector
None