class CustomFlowLayout: UICollectionViewFlowLayout {
var attributeArr: [UICollectionViewLayoutAttributes] = []
var totalHeight: CGFloat = 0
//insetArr(用来没放每组中的上线左右的距离)
var insetDict: [Int: UIEdgeInsets] = [:]
var minLineDict: [Int: CGFloat] = [:]
var minInterDict: [Int: CGFloat] = [:]
override func prepare() {
super.prepare()
guard let collectionView = self.collectionView else { return }
let sections = self.collectionView?.numberOfSections ?? 0
///左边距离的位置
var leftWidth: CGFloat = 0
var superWidth: CGFloat = collectionView.bounds.width
var leaveWidth: CGFloat = superWidth
for indexPathSection in 0..<sections {
let numbersOfSection = self.collectionView?.numberOfItems(inSection: indexPathSection) ?? 0
let inset = insetDict[indexPathSection] ?? UIEdgeInsets.zero
let minInter = minInterDict[indexPathSection] ?? 0
let minLine = minLineDict[indexPathSection] ?? 0
//设置组头的布局
if let headerAttribute = self.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath.init(item: 0,section: indexPathSection)) {
headerAttribute.frame = CGRect.init(x: 0, y: totalHeight, width: headerAttribute.frame.width, height: headerAttribute.frame.height)
totalHeight += inset.top + headerAttribute.frame.height
attributeArr.append(headerAttribute)
}
for indexPathItem in 0..<numbersOfSection {
let indexPath = IndexPath.init(item: indexPathItem, section: indexPathSection)
let itemSize = self.layoutAttributesForItem(at: indexPath)?.size ?? CGSize.zero
if let cellAttribute = self.layoutAttributesForItem(at: indexPath) {
if indexPath.item == 0 {
leftWidth = inset.left
leaveWidth = superWidth - leftWidth - inset.right
}
if leaveWidth < itemSize.width {
leftWidth = inset.left
totalHeight += itemSize.height + minLine
leaveWidth = superWidth - leftWidth - inset.right
}
cellAttribute.frame = CGRect.init(x: leftWidth, y: totalHeight, width: itemSize.width, height: itemSize.height)
leftWidth += minInter + itemSize.width
leaveWidth = superWidth - leftWidth - inset.right
attributeArr.append(cellAttribute)
if indexPathItem == (numbersOfSection - 1) {
totalHeight += itemSize.height
}
}
}
//设置组尾布局
if let footerAttribure = self.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, at: IndexPath.init(item: 0, section: indexPathSection)) {
footerAttribure.frame = CGRect.init(x: 0, y: totalHeight + inset.bottom, width: footerAttribure.frame.width, height: footerAttribure.frame.height)
totalHeight += footerAttribure.frame.height + inset.bottom
attributeArr.append(footerAttribure)
}
}
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return self.attributeArr
}
// override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
// return true
// }
override var collectionViewContentSize: CGSize{
get {
return CGSize.init(width: self.collectionView?.bounds.width ?? 0, height: self.totalHeight)
}
}
}
自定义collectionviewflowLayout
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 这次回学校做毕业设计准备毕业了,突然觉得时间过的好快啊,忙完了之后今天终于有时间写点东西了.然后就写了这个自定义c...
- newEvent 自定义事件 newEvent(typeArg,eventInit) typeArg事件名称 ev...
- 转载请注明出处:http://blog.csdn.net/linglongxin24/article/detail...
- 前言 在开发自定义view时,往往需要绑定xml布局文件,那具体怎么做呢? 自定义view和xml布局文件关联的思...
- 在上篇中,我与大家分享了关于如何进行*.lrc歌词文件的解析,以及将解析完成后的歌词展示在镶嵌在ScrollVie...