[英]Disable dynamic sizing in UICollectionViewFlowLayout
我在 UICollectionView 中分配了一個 UICollectionViewFlowLayout。 它的委托方法在 sizeForItemAtIndexPath 中返回固定的 CGSize。 問題是如果 UICollectionViewCell 包含 UIImageView 或任何其他動態大小的視圖,其自動布局約束與 contentView 匹配前導、尾隨、頂部、底部,則單元格會根據 imageView 中圖像的大小調整大小。 是否可以使用 UICollectionViewFlowLayout 獲得固定大小的單元格? 我嘗試在情節提要中為 UIImageView 設置intrinsicContentSize,但它不起作用。
使用代碼添加更多詳細信息:
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.
我不知道這是否是 iOS 中的一個已知錯誤,但在 Storyboard 中將 UICollectionViewFlowLayout 的estimatedSize 設置為 .none 可以解決問題,如 Apple Developer Forums 中的答案所述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.