gpt4 book ai didi

ios - 将分页UICollectionView高度调整为当前可见的UICollectionViewCell

转载 作者:行者123 更新时间:2023-12-01 21:31:58 25 4
gpt4 key购买 nike

我在UIScrollView内有一个带有水平FlowLayout的分页UICollectionView(上面和下面有更多内容),一次只显示一个UICollectionViewCell。每个单元都有全宽,但动态高度。现在,无论何时更改单元格,我都想调整UICollectionView的高度。
我已经有一个方法,当当前或下一个“页面”更改更新UICollectionView的高度约束时,可以调用该方法。如果下一个“页面”比上一个页面高,则已经开始工作,但是当下一个页面比当前页面小时,它会完全混乱。
GIF of current state (too big to include)

private func updateCardHeight() {
guard let currentHeight: CGFloat = cardCollectionView.layoutAttributesForItem(at: currentIndexPath)?.frame.height,
let nextHeight: CGFloat = cardCollectionView.layoutAttributesForItem(at: nextIndexPath)?.frame.height
else { return }
let height = max(currentHeight, nextHeight)

guard cardsHeightConstraint.constant != height else { return }
cardsHeightConstraint.constant = height

UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: [], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
在某些时候 layoutAttributesForItem(at:)失败,并返回 50的固定高度,而不是大约的高度。 300。有没有人尝试过类似的东西-我找不到任何东西,有点迷路了。
谢谢你的帮助!

最佳答案

@Hannes,我在您的项目中看到了问题(项目高度必须小于UICollectionView的高度)。有多种方法可以实现它,但目前,我可以建议修复它的最小方法。您可以实现“ UICollectionViewDelegateFlowLayout ”,然后尝试以下代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return self.cardCollectionView.frame.size
}
希望它对您有所帮助。

关于ios - 将分页UICollectionView高度调整为当前可见的UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62929381/

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