Пожалуйста, следуйте инструкциям, чтобы установить PinterestLayout
в UICollectionView
ниже.
Установите PinterestLayout
для просмотра вашей коллекции.
let layout = PinterestLayout()
collectionView.collectionViewLayout = layout
Макет установки
layout.delegate = self
layout.cellPadding = 5
layout.numberOfColumns = 2
Реализация методов PinterestLayoutDelegate
extension CustomCollectionVC: PinterestLayoutDelegate {
func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat {
let image = images[indexPath.item]
return image.height(forWidth: withWidth)
}
func collectionView(collectionView: UICollectionView,
heightForAnnotationAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat {
let textFont = UIFont(name: "Arial-ItalicMT", size: 11)!
return "Some text".heightForWidth(width: withWidth, font: textFont)
}
}
Когда данные загружены, сделать макет недействительным, а также перезагрузить данные в представлении сбора.
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()
Наблюдение:
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat
Попробуйте это вместо вышеуказанного:
func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat