サブクラス化UICollectionViewFlowLayout
によって
let flowLayout = LeftAgignedFlowLayout()
flowLayout.minimumLineSpacing = 18
flowLayout.minimumInteritemSpacing = 8
flowLayout.scrollDirection = .vertical
self.collectionViewLayout = flowLayout
class LeftAgignedFlowLayout : UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let arr = super.layoutAttributesForElements(in: rect)!
return arr.map {
atts in
var atts = atts
if atts.representedElementCategory == .cell {
let ip = atts.indexPath
atts = self.layoutAttributesForItem(at:ip)!
}
return atts
}
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
var atts = super.layoutAttributesForItem(at:indexPath)!
if indexPath.item == 0 {
return atts
}
if atts.frame.origin.x - 1 <= self.sectionInset.left {
return atts
}
let ipPv = IndexPath(item:indexPath.row-1, section:indexPath.section)
let fPv = self.layoutAttributesForItem(at:ipPv)!.frame
let rightPv = fPv.origin.x + fPv.size.width + self.minimumInteritemSpacing
atts = atts.copy() as! UICollectionViewLayoutAttributes
atts.frame.origin.x = rightPv
return atts
}
}
を、それを使用してみてください私はそれが非常に些細な質問だ知っているが、私はそれを聞いてみろ。あなたのコレクションビューのレイアウトにカスタムクラスを正しく割り当てましたか? – Adeel
質問してくれてありがとう:-)時間があれば、このPlaygroundを見てみましょう:https://github.com/JCzz/CollectionView全体的に見て、画面の中央にViewを拡大しようとしています。 –
@ ChrisG.私は同じ問題を抱えています。 "LayoutAttributesForItem"がまったく起動しないことを除いて、すべてうまくいくようです。なぜ私は分からない。 – jnel899