如何在UICollectionView中添加HeaderView,如UITableView的tableHeaderView

Chr*_*sen 35 iphone header uitableview ios uicollectionview

如何在顶部添加标题视图/顶视图(不是节标题)UICollectionView

它应该excactly充当UITableViewtableHeaderView财产.

因此,它需要位于第一部分标题视图的顶部(在索引0处的部分之前),与其余内容一起滚动,并进行用户交互.

到目前为止,我提出的最好的方法是为第一个标题视图创建一个特殊的XIB(带有作为File所有者的MyCollectionReusableView子类UICollectionReusableView),该视图足够大,也可以在标题中包含我的子视图,这是一种黑客攻击,我我想,我还没有设法检测到触摸.

不确定我是否可以让我的MyCollectionReusableView子类允许触摸或者有更好的方法.

小智 55

self.collectionView  =  [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) collectionViewLayout:flowlayout];
self.collectionView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
UIImageView *imagev = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"015.png"]];
imagev.frame = CGRectMake(0, -50, 320, 50);
[self.collectionView addSubview: imagev];
[self.view addSubview: _collectionView];
Run Code Online (Sandbox Code Playgroud)

我使用该属性contentInset将框架插入到顶部UICollectionView,然后我将图像视图添加到它,并且它成功.我认为它可以作为excactly行动UITableViewtableHeaderView财产.你怎么看?


Tom*_*nko 10

从 iOS 13 开始,设置标题的规范方法是使用UICollectionViewCompositionalLayoutConfiguration

这种方式允许boundarySupplementaryItems为集合视图按部分或全局进行设置。

let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44))

let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: "header", alignment: .top)

let config = NSCollectionViewCompositionalLayoutConfiguration()
config.boundarySupplementaryItems = [header]

let layout = UICollectionViewCompositionalLayout(sectionProvider: sectionProvider, configuration: config)

return layout
Run Code Online (Sandbox Code Playgroud)

要了解更多信息,请访问官方文档:https ://developer.apple.com/documentation/appkit/nscollectionviewcompositionallayoutconfiguration


rde*_*mar 7

我通过添加UIView作为集合视图的子视图,将视图放在集合视图的顶部.它确实向上滚动集合视图,它具有正常的UIView用户交互.如果您没有节标题,这可以正常工作,但如果您没有,则无效.在那种情况下,我认为你做这件事的方式没有任何问题.我不确定你为什么没有检测到触摸,它们对我来说很好.


Swi*_*bit 5

我最终使用 UICollectionView 扩展来添加我的自定义标题。使用特定的标签,我可以伪造存储的属性来识别我的自定义标头(所有外部都是透明的)。如果在添加自定义标题时 UICollectionView 的布局未完成,您可能需要滚动到特定的偏移量。

extension UICollectionView {
    var currentCustomHeaderView: UIView? {
        return self.viewWithTag(CustomCollectionViewHeaderTag)
    }

    func asssignCustomHeaderView(headerView: UIView, sideMarginInsets: CGFloat = 0) {
        guard self.viewWithTag(CustomCollectionViewHeaderTag) == nil else {
            return
        }
        let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
        headerView.frame = CGRect(x: sideMarginInsets, y: -height + self.contentInset.top, width: self.frame.width - (2 * sideMarginInsets), height: height)
        headerView.tag = CustomCollectionViewHeaderTag
        self.addSubview(headerView)
        self.contentInset = UIEdgeInsets(top: height, left: self.contentInset.left, bottom: self.contentInset.bottom, right: self.contentInset.right)
    }

    func removeCustomHeaderView() {
        if let customHeaderView = self.viewWithTag(CustomCollectionViewHeaderTag) {
            let headerHeight = customHeaderView.frame.height
            customHeaderView.removeFromSuperview()
            self.contentInset = UIEdgeInsets(top: self.contentInset.top - headerHeight, left: self.contentInset.left, bottom: self.contentInset.bottom, right: self.contentInset.right)
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

CustomCollectionViewHeaderTag 是指您为标题提供的标签编号。确保它不是嵌入在 UICollectionView 中的 anotherView 的标签。