我在UICollectionView上分配了一个UICollectionView。它的委托方法返回sizeForItemAtIndexPath中的固定sizeForItemAtIndexPath。问题是,如果UICollectionViewCell包含UIImageView或任何其他具有自动输出约束的动态视图,这些约束与contentView的前导、尾随、顶部和底部相匹配,则单元格将根据imageView中图像的大小调整大小。用UICollectionViewFlowLayout可以得到固定大小的单元吗?我尝试在故事板中为intrinsicContentSize设置UIImageView,但它不起作用。
使用代码添加更多详细信息:
let numberOfItemsPerRow = 3
let spacingBetweenCells = CGFloat(40.0)
let sizeOfItem = CGFloat(210.0)
var sectionInsets:UIEdgeInsets {
let margin:CGFloat = collectionView.bounds.width - CGFloat(numberOfItemsPerRow) * sizeOfItem - CGFloat(numberOfItemsPerRow - 1) * spacingBetweenCells
return UIEdgeInsets(top: 30, left: margin/2, bottom: 30, right: margin/2)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
NSLog("Cell size \(sizeOfItem)")
return CGSize(width: sizeOfItem, height: sizeOfItem)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return spacingBetweenCells
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mediaCell", for: indexPath) as! ThumbViewCell
cell.mediaItem = mediaItems[indexPath.item]
if let thumbURL = mediaItem.thumbURL, let image = UIImage(contentsOfFile: thumbURL.path) {
cell.thumbnailView.image = image
cell.thumbnailView.contentMode = .scaleAspectFill
}
return cell
}
这是输出。它明显地与图像大小成比例。
下面是控制台日志:
2022-06-04 11:47:34.599808+0400 SmrtImageCam[17794:1098605] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x155d0d9c0>, and it is attached to <UICollectionView: 0x156023400; frame = (262.5 0; 931.5 834); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x280438690>; layer = <CALayer: 0x280a7f560>; contentOffset: {0, -50}; contentSize: {931.5, 770}; adjustedContentInset: {50, 0, 20, 0}; layout: <UICollectionViewFlowLayout: 0x155d0d9c0>; dataSource: <SmrtImageCam.LibraryController: 0x1574082e0>>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
The behavior of the UICollectionViewFlowLayout is not defined because: the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
Please check the values returned by the delegate.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x155d0d9c0>, and it is attached to <UICollectionView: 0x156023400; frame = (262.5 0; 931.5 834); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x280438690>; layer = <CALayer: 0x280a7f560>; contentOffset: {0, -50}; contentSize: {931.5, 770}; adjustedContentInset: {50, 0, 20, 0}; layout: <UICollectionViewFlowLayout: 0x155d0d9c0>; dataSource: <SmrtImageCam.LibraryController: 0x1574082e0>>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
发布于 2022-06-04 08:27:10
我不知道它在iOS中是否是一个已知的错误,但是在Storyboard中将estimatedSize设置为.none for UICollectionViewFlowLayout可以解决这个问题,就像在苹果开发者论坛上这应答中所描述的那样。
https://stackoverflow.com/questions/72492619
复制相似问题