如何更改 collectionview 单元格的大小?
How can I change the size of a collectionview's cells?
我的 collectionview 开始是这样的:
initial screen
当用户在集合上向上滑动时,它会像这样展开:
second screen
如何在集合视图展开时更改单元格的大小?
英文:
您需要将单元格的高度设置为集合视图的高度,并且每次调整集合大小时都需要刷新布局。
使用iOS语言
您需要使您的视图控制器符合 UICollectionViewDelegateFlowLayout
并使用
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
调整单元格大小
extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = collectionView.frame.size.height
let width = //make your calculation
return CGSize(width: width, height: height)
}
}
像这样
我的 collectionview 开始是这样的: initial screen
当用户在集合上向上滑动时,它会像这样展开: second screen
如何在集合视图展开时更改单元格的大小?
英文:
您需要将单元格的高度设置为集合视图的高度,并且每次调整集合大小时都需要刷新布局。
使用iOS语言
您需要使您的视图控制器符合 UICollectionViewDelegateFlowLayout
并使用
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
调整单元格大小
extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = collectionView.frame.size.height
let width = //make your calculation
return CGSize(width: width, height: height)
}
}
像这样