首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >禁用UICollectionViewFlowLayout中的动态大小调整

禁用UICollectionViewFlowLayout中的动态大小调整
EN

Stack Overflow用户
提问于 2022-06-03 16:32:34
回答 1查看 172关注 0票数 0

我在UICollectionView上分配了一个UICollectionView。它的委托方法返回sizeForItemAtIndexPath中的固定sizeForItemAtIndexPath。问题是,如果UICollectionViewCell包含UIImageView或任何其他具有自动输出约束的动态视图,这些约束与contentView的前导、尾随、顶部和底部相匹配,则单元格将根据imageView中图像的大小调整大小。用UICollectionViewFlowLayout可以得到固定大小的单元吗?我尝试在故事板中为intrinsicContentSize设置UIImageView,但它不起作用。

使用代码添加更多详细信息:

代码语言:javascript
运行
复制
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 {

代码语言:javascript
运行
复制
    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
}

这是输出。它明显地与图像大小成比例。

下面是控制台日志:

代码语言:javascript
运行
复制
 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.
EN

回答 1

Stack Overflow用户

发布于 2022-06-04 08:27:10

我不知道它在iOS中是否是一个已知的错误,但是在Storyboard中将estimatedSize设置为.none for UICollectionViewFlowLayout可以解决这个问题,就像在苹果开发者论坛上应答中所描述的那样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72492619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档