• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 向 ray wenderlich Pinterest 布局添加自定义标题

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:19:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用了 Ray Wenderlich 提供的 Pinterest 布局文件,并完美地实现了所需的单元格布局,但是在我添加自定义标题的 CollectionViewController 上,我想在自定义标题下方添加 Pinterest 层。
但是 Pinterest 层覆盖了自定义 header ,我再也找不到自定义 header 。

任何关于我如何解决这个问题的建议将不胜感激。

Pinterest 布局文件如下:

import UIKit

protocol CustomLayoutDelegate: class {
    // 1. Method to ask the delegate for the height of the image
    func collectionView(_ collectionView:UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat
}

class CustomLayout: UICollectionViewFlowLayout {

//1. Pinterest Layout Delegate
weak var delegate: CustomLayoutDelegate!

//2. Configurable properties
fileprivate var numberOfColumns = 2
fileprivate var cellPadding: CGFloat = 5

//3. Array to keep a cache of attributes.
fileprivate var cache = [UICollectionViewLayoutAttributes]()

//4. Content height and size
fileprivate var contentHeight: CGFloat = 0

fileprivate var contentWidth: CGFloat {
    guard let collectionView = collectionView else {
        return 0
    }
    let insets = collectionView.contentInset
    return collectionView.bounds.width - (insets.left + insets.right)
}

override var collectionViewContentSize: CGSize {
    return CGSize(width: contentWidth, height: contentHeight)
}

override func prepare() {
    // 1. Only calculate once
    guard cache.isEmpty == true, let collectionView = collectionView else {
        return
    }
    // 2. Pre-Calculates the X Offset for every column and adds an array to increment the currently max Y Offset for each column
    let columnWidth = contentWidth / CGFloat(numberOfColumns)
    var xOffset = [CGFloat]()
    for column in 0 ..< numberOfColumns {
        xOffset.append(CGFloat(column) * columnWidth)
    }
    var column = 0
    var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)

    // 3. Iterates through the list of items in the first section
    for item in 0 ..< collectionView.numberOfItems(inSection: 0) {

        let indexPath = IndexPath(item: item, section: 0)

        // 4. Asks the delegate for the height of the picture and the annotation and calculates the cell frame.
        let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)
        let height = cellPadding * 2 + photoHeight
        let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height)
        let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)

        // 5. Creates an UICollectionViewLayoutItem with the frame and add it to the cache
        let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
        attributes.frame = insetFrame
        cache.append(attributes)

        // 6. Updates the collection view content height
        contentHeight = max(contentHeight, frame.maxY)
        yOffset[column] = yOffset[column] + height

        column = column < (numberOfColumns - 1) ? (column + 1) : 0
    }
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

    var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]()

    // Loop through the cache and look for items in the rect
    for attributes in cache {
        if attributes.frame.intersects(rect) {
            visibleLayoutAttributes.append(attributes)
        }
    }
    return visibleLayoutAttributes
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    return cache[indexPath.item]
}

}



Best Answer-推荐答案


您必须在附加第一项之前为您的标题附加一个属性

除此之外,首先从您的页眉高度开始设置 yOffset

var yOffset = [yourHeaderHeight, yourHeaderHeight]

在第 3 节中 indexPath 之后的节之间添加此代码

 if item == 0 {
                let headerAttributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: indexPath)
                headerAttributes.frame = CGRect(x: 0, y: 0, width: yourHeaderWidth, height: yourHeaderHeight)

                cache.append(headerAttributes)
            }

关于ios - 向 ray wenderlich Pinterest 布局添加自定义标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51134275/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap