gpt4 book ai didi

ios - 设备旋转后调用 invalidateLayout() 或 reloadData() 重新加载从 Storyboard 创建的 UICollectionView

转载 作者:行者123 更新时间:2023-11-29 05:50:03 26 4
gpt4 key购买 nike

UITableViewControllerUITableViewCell 中,我有一个网格,每行具有固定数量的静态 UICollectionViewCells(无间距)。

extension NounsTVC: UICollectionViewDataSource {

func collectionView( _ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellIds.count
}

func collectionView( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell( withReuseIdentifier: cellIds[indexPath.item], for: indexPath)
cell.layer.borderColor = UIColor.gray.cgColor
cell.layer.borderWidth = 0.5
return cell
}
}

extension NounsTVC: UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let availableWidth = collectionView.bounds.size.width
let minimumWidth = floor(availableWidth / cellsPerRow)
let remainder = availableWidth - minimumWidth * CGFloat(cellsPerRow)

let columnIndex = indexPath.row % Int(cellsPerRow)
let cellWidth = CGFloat(columnIndex) < remainder ? minimumWidth + 1 : minimumWidth
return CGSize(width: cellWidth, height: 45)
}
}

工作正常。

但是,当我旋转设备时,它需要调用这些布局方法。

这看起来像答案:Swift: How to refresh UICollectionView layout after rotation of the device

但是我没有将 UICollectionView 作为 invalidLayout() 因为 UICollectionView 是在 IB 中创建的(据我所知),那么如何我要在 UICollectionView 上调用 invalidateLayout 吗?

最佳答案

在上述情况中发现了两个问题。

首先,xcode 没有通过在 Storyboard和 .swift 文件之间拖动一条线来在我的自定义 UITableViewCell 中创建 IBOutlet(显然是某些星座中的错误)。解决方案是手动输入代码,然后按住 Ctrl 键并将其反向拖放到 Storyboard中的正确 View 。

@IBOutlet weak var myCollectionView: UICollectionView!

其次,由于 Collection View 是在自定义 UITableViewCell 中实例化的,因此无法使用 viewWillTransitionToSize:withTransitionCoordinator: 或 viewWillLayoutSubviews ( TableView 方法)。但是,UITableViewCell 确实有以下内容,其中可以调用 .invalidateLayout():

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
{
super.traitCollectionDidChange(previousTraitCollection)
guard previousTraitCollection != nil else
{ return }
myCollectionView.collectionViewLayout.invalidateLayout()
}

关于ios - 设备旋转后调用 invalidateLayout() 或 reloadData() 重新加载从 Storyboard 创建的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55760463/

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