ios - 如何实现顶部带有标题的水平滚动 Collection View ?

标签 ios swift uicollectionview uicollectionviewcell

我想要一个水平滚动的集合 View ,但我也希望它在顶部和右侧有一个标题。我已经尝试像垂直滚动集合 View 一样实现它,并且标题最终位于第一个单元格的左侧。

这是我如何使 header 出列:

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier, for: indexPath) as! HeaderView
        header.setWidth(to: headerWidth)
        return header
    }

标题大小:
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 12)
    }

除此之外,我初始化的唯一其他属性是集合 View 的滚动方向和插图

最佳答案

标题位置:在 顶部 在垂直滚动的情况下和 中的部分左 在水平滚动的情况下的部分,因此在您的情况下它显示在部分的左侧是正常的。

如果您想显示您的 reusable view在每个部分的右侧您可以使用 UICollectionView.elementKindSectionFooter 反而 。

但是如果你想显示你的reusable view在每个部分之间,您可以将第一部分中标题 View 的大小设置为 .zero 。

您可以使用 referenceSizeForHeaderInSection 在每个标题 View 大小中进行操作功能如下:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 {
            return .zero
        }
        return CGSize(width: collectionView.frame.width, height: 12)
    }

在这种情况下,我删除了(技术上没有删除而是隐藏)左侧的第一个标题,它位于第一部分,但是您可以设置每个标题的大小。

关于ios - 如何实现顶部带有标题的水平滚动 Collection View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60428393/

相关文章:

Swift 中的 Objective-C block - 变量丢失?

ios - 为什么我不能将 UICollectionViewFlowLayout 中的 referenceSizeForFooterInSection 与 EstimateItemSize 一起使用?

ios - UICollectionView Xamarin.iOS,带按钮的自定义单元格

充电时的IOS通知

iphone - 防止 UIViewController 从应用程序包中加载陈旧的 XIB

ios - 如何在 iOS 应用中共享 Firebase 身份验证数据?

ios - UICollectionViewCellawakeFromNib 从未被调用,即使 registerNib 被调用

ios - 为什么在使用 CGAffineTransform MakeRotation 旋转时 View 会改变位置

ios - RestKit 核心数据映射一对多

objective-c - 如何在 Swift 中创建立即调用的函数( block )