在 Swift 4 中垂直调整 .xib(单元格)的大小
Resizing the .xib (cell) vertically in Swift 4
我创建了一个显示商店图像和商店信息的 .xib(单元格)。
灰色是图像中未调整大小的部分,是一个 UICollectionView。
所有商店都有不同的大小,但在 UICollectionView(显示图像)中,无论大小如何,它们都显示为 Scale To Fill。
我希望 .xib 垂直尺寸 (UICollectionView) 可以增大和缩小以适应图像的垂直尺寸。我该怎么办?
如果您正在处理 UICollectionViewCell
,您并不需要直接 change/update xib 高度或大小。更重要的是你需要的是 CollectionViewFlowLayout
方法。
我想你需要的是
1. In your xib, you should properly layout your items with `Autolayout`.
2. Make sure that you set the `collectionview.delegate = self` somewhere based on your need
3. Add `UICollectionViewDelegateFlowLayout` methods and return proper values.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: `width`, height: `height`)
}
上述委托方法应该足够了,但您可能需要根据需要使用其他方法。
我创建了一个显示商店图像和商店信息的 .xib(单元格)。
灰色是图像中未调整大小的部分,是一个 UICollectionView。
所有商店都有不同的大小,但在 UICollectionView(显示图像)中,无论大小如何,它们都显示为 Scale To Fill。
我希望 .xib 垂直尺寸 (UICollectionView) 可以增大和缩小以适应图像的垂直尺寸。我该怎么办?
如果您正在处理 UICollectionViewCell
,您并不需要直接 change/update xib 高度或大小。更重要的是你需要的是 CollectionViewFlowLayout
方法。
我想你需要的是
1. In your xib, you should properly layout your items with `Autolayout`.
2. Make sure that you set the `collectionview.delegate = self` somewhere based on your need
3. Add `UICollectionViewDelegateFlowLayout` methods and return proper values.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: `width`, height: `height`)
}
上述委托方法应该足够了,但您可能需要根据需要使用其他方法。