您可以检查是否选择了indexPath
来自sizeForItemAt
.如果是,则返回不同的高度,否则返回标准高度.
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { collectionView.performBatchUpdates(nil, completion: nil) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch collectionView.indexPathsForSelectedItems?.first { case .some(indexPath): return CGSize() // your selected height default: let height = (view.frame.width) * 9 / 16 return CGSize(width: view.frame.width, height: height + 50 + 50) } }