【问题标题】:CollectionView sizeForitemAt method not working for ios 9CollectionView sizeForitemAt 方法不适用于 ios 9
【发布时间】:2018-05-26 23:22:20
【问题描述】:

我遇到了奇怪的问题,我不确定为什么会发生这种情况。您可以看到这两张图片,一张是运行 ipad 的 ios 9,另一张是运行 ipad 的 ios11。对于 ios9 正在运行的 ipad 单元格不会调整大小。我搜索了很多,但没有得到答案。请帮我解决这个问题,我还附上了代码。

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! WalkthroughCollectionViewCell

    let walkObject = walkObjects[indexPath.row]

    cell.walkImageView.image = walkObject.image
    cell.walkHeader.text = walkObject.Header
    cell.walkDetails.text = walkObject.Details
    cell.getStartedButton.addTarget(self, action: #selector(getStarted(sender:)), for: .touchDown)

    if (indexPath.row + 1) == walkObjects.count {
        cell.getStartedButton.isHidden = false
    }else{
        cell.getStartedButton.isHidden = true
    }

    return cell
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}


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

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

【问题讨论】:

  • 你需要实现 UICollectionViewDelegateFlowLayout
  • 我已经添加了 extension WalkthroughCollectionView: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  • 你确定程序运行时调用了这个方法
  • 是的,@Vikky 我检查了

标签: ios swift ipad uicollectionview


【解决方案1】:

就我而言,我将 Estimate SizeAutomatic 更改为 None 并使用:

 collectionView(_ collectionView: UICollectionView, layout
 collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath:
 IndexPath) -> CGSize

【讨论】:

  • 你拯救了我的一天
  • 哇。 ...只是...哇。什么时候被调用的代码不再是事实的来源变得正常了?故事板设置会覆盖代码,这很糟糕。谢谢,这行得通。
【解决方案2】:

好的,我不知道为什么,但它现在可以工作了。我只是将 NavigationBarHidden 设置为 false。它开始工作了。

【讨论】:

  • 是的,因为我不想在这个视图中显示导航栏,这就是我设置隐藏的原因,但现在我认为我只需要为旧版本显示它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多