gpt4 book ai didi

ios - UICollectionViewCell sizeForItemAt 值以某种方式恢复为 fitItemSize?

转载 作者:行者123 更新时间:2023-11-28 20:49:56 24 4
gpt4 key购买 nike

我在 viewcontroller 中设置了 2 个 collectionView,它们都从端点获取数据,然后 reloadData()。

一个 collectionView 就像一个标题选项卡,其单元格大小取决于其内部大小,并依靠 insetForSection 将单元格定位/对齐到 collectionView 的中心。

另一个为自己设置“某种”固定大小,其中第一个单元格几乎是 collectionView 的整个宽度,然后第一个单元格之后的单元格将占据 collectionView 宽度的一半。

我已经设置了委托(delegate)和扩展方法,但是由于某种原因,专注于第二个 CollectionView 的 sizeforItem 没有“坚持”,它们被还原了。当我这样做时:

 self.statusOptionCollectionView.reloadData()
self.statusOptionCollectionView.performBatchUpdates({
self.statusOptionCollectionView.layoutIfNeeded()
}) { (complete) in
debugPrint("Batch Update complete")
}

我看到了我想要的结果的简短框架,但随后 collectionView 突然撤消了我的 sizeForItem 代码并将单元格更改为类似于“适合大小”的内容。 (图片:下方)。

问题是我该如何解决这个问题?是什么原因造成的?是因为我在一个 View Controller 中有 2 个 collectionView 吗?我尝试在 viewdidlayoutsubviews 中使用 invalidatingLayout 但它不起作用。 “我确实使用了 Storyboard,但我已经删除了 collectionView 并重新添加了它,但没有修复它)

我想要这样的东西(关注第二个 View Controller 布局): enter image description here

我的 CollectionView 布局代码是这样的(kindTabCollectionView 是“标题”,居中对齐):

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if collectionView.isEqual(self.kindTabCollectionView){
let layout = collectionViewLayout as! UICollectionViewFlowLayout
let totalCellWidth = layout.itemSize.width * CGFloat(self.kindArray.count)
let totalSpacingWidth = CGFloat(8 * (self.kindArray.count - 1))
let leftInset = (collectionView.bounds.size.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
let rightInset = leftInset
return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}else{
return UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
}
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView.isEqual(self.kindTabCollectionView){
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
return flowLayout.itemSize
}else{
let height = CGFloat(40.0)
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let widthMargin = (flowLayout.sectionInset.left + flowLayout.sectionInset.left + flowLayout.minimumInteritemSpacing)
if indexPath.item == 0 && indexPath.section == 0{
let width = floor(collectionView.frame.size.width - widthMargin)
return CGSize(width: width, height: height)
}else{
let width = floor((collectionView.frame.size.width / 2) - widthMargin)
return CGSize(width: width, height: height)
}
}
}

然而,出来的结果是这样的:(抱歉,只有几帧,我尽力截屏了,但它确实尝试更改为正确的帧大小,但随后又恢复为小的“fitToSize”图片)

enter image description here enter image description here

最佳答案

在尺寸检查器( Storyboard)中检查 collectionView 的“估计尺寸”属性。当使用 UICollectionViewDelegateFlowLayout 的扩展来设置单元格的大小时,它应该设置为“无”。

如 Xcode 11 发行说明中所述:

Cells in a UICollectionView can now self size with Auto Layout constrained views in the canvas. To opt into the behavior for existing collection views, enable “Automatic” for the collection view’s estimated size, and “Automatic” for cell’s size from the Size inspector. If deploying before iOS 13, you can activate self sizing collection view cells by calling performBatchUpdates(_:completion:) during viewDidLoad(). (45617083)

因此,新创建的 collectionViews 将属性“估计大小”设置为“自动”,并且单元格的大小是根据其 subview 尺寸计算的,因此忽略了 UICollectionViewDelegateFlowLayout 扩展方法,即使它们被调用也是如此。

关于ios - UICollectionViewCell sizeForItemAt 值以某种方式恢复为 fitItemSize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59101947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com