Skip to content

Instantly share code, notes, and snippets.

@hsleedevelop
Last active November 16, 2018 05:37
Show Gist options
  • Save hsleedevelop/d1612838eadcbe38429866e1e91b50d9 to your computer and use it in GitHub Desktop.
Save hsleedevelop/d1612838eadcbe38429866e1e91b50d9 to your computer and use it in GitHub Desktop.
a sample for using UICollectionViewFlowLayout
class AlignTopCollectionViewFlowLayout: UICollectionViewFlowLayout {
var sectionLayout: HomeSectionLayout?
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attrs = super.layoutAttributesForElements(in: rect)
var attrsCopy = [UICollectionViewLayoutAttributes]()
let layout = sectionLayout ?? .PERSONAL
for element in attrs! {
let elementCopy = element.copy() as! UICollectionViewLayoutAttributes
if (elementCopy.representedElementCategory == .cell) {
if layout == .EDITOR {//2 items for line
var offset = elementCopy.frame.origin.y
let yyy = (elementCopy.frame.origin.y - elementCopy.frame.size.height)
let divide = yyy / self.minimumLineSpacing
let adjust = (self.minimumLineSpacing * divide)
Log.d("layout == .EDITOR || layout == .SHORTCUT >> \(offset) >> \(yyy) >> \(self.minimumLineSpacing) >> divide >> \(divide) >> \(adjust)")
offset = offset - (self.minimumLineSpacing * divide)
elementCopy.frame.origin.y = offset
}
if layout == .EDITOR || layout == .SHORTCUT {//
var count = collectionView?.numberOfItems(inSection: 0) ?? 0
count = (count / 2 + count % 2) - 1 // subtract 1 line = 3 ==> 2
let bottomMarginOfCollectionView = HomeSectionTableViewCell.margin / count.c //uicollectionview의 UICollectionViewFlowLayoutBreakForInvalidSizes 회피
let divide = elementCopy.frame.origin.y / (elementCopy.frame.size.height + self.minimumLineSpacing) //calculate about line index
let adjustOffset = (bottomMarginOfCollectionView + self.minimumLineSpacing) * divide
let offset = (elementCopy.frame.origin.y - adjustOffset)
let adjust = (self.minimumLineSpacing * divide)
Log.d("layout == .EDITOR || layout == .SHORTCUT >> \(offset) >> \(yyy) >> \(self.minimumLineSpacing) >> divide >> \(divide) >> \(adjust)")
elementCopy.frame.origin.y = offset
} else {
elementCopy.frame.origin.y = 0
}
}
attrsCopy.append(elementCopy)
}
return attrsCopy
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment