【问题标题】:UICollectionViewController: make sticky footer, regardless of size of CollectionViewUICollectionViewController:无论 CollectionView 的大小如何,都制作粘性页脚
【发布时间】:2017-08-10 23:27:41
【问题描述】:

我正在使用两个补充视图(即UICollectionReusableViews)来呈现夹着 UICollectionView 的页脚和页眉。'

问题是当UICollectionView 很短(例如,只占用一个单元格)时,页脚视图会向上滑动,并且不会“粘”到底部!

其他文章已经解决了这种行为;在某些情况下,作者使用自定义布局来实现粘性页脚/页眉。相反,我只是将sectionFootersPinToVisibleBoundssectionHeadersPinToVisibleBounds 设置为true,这通常可以正常工作——直到UICollectionView 变短。

这发生在 Swift 3.0 (iOS 10.3)、iPhone 6.x 模拟器中。

将页脚视图固定到UICollectionViewController 底部的推荐方法是什么?

【问题讨论】:

    标签: ios swift3


    【解决方案1】:

    如果您不坚持使用 UICollectionViewController,我通常会使用常规的 'ol UIViewController 并在其中粘贴 UICollectionView,扩展 UICollectionViewDataSource 和 UICollectionViewDelegate 协议。

    我想如果你需要 UICollectionViewController,你可以做同样的事情,但嵌入视图控制器本身。

    使用 UIViewController 可以很容易地通过约束将视图粘贴到您想要的位置。

    【讨论】:

    • 我们可能最终会听从您的建议。我选择 UICollectionViewController 的“内置”页脚/页眉的原因是开箱即用,该控制器可以很好地解决我们的问题。我认为通用 UIViewController 可能会正常工作,但可能需要更多的脚力......
    【解决方案2】:

    好的..所以我尝试从下面的链接更新代码。并且有效。

    https://teamtreehouse.com/community/add-sticky-footer-to-uicollectionview-in-swift

    class StickyFooter : UICollectionViewFlowLayout {
    
    
    var footerIsFound             : Bool = false
    var UICollectionAttributes    : [UICollectionViewLayoutAttributes]?
    
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
    {
        UICollectionAttributes = super.layoutAttributesForElements(in: rect)
    
        for  attributes in UICollectionAttributes! {
    
            if let type = attributes.representedElementKind {
    
                if type == UICollectionElementKindSectionFooter
                {
                    footerIsFound = true
                    updateFooter(attributes: attributes)
                }
            }
        }
    
        if (!self.footerIsFound) {
    
            let newItem = self.layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionFooter, at : NSIndexPath(row: self.UICollectionAttributes!.count, section: 0) as IndexPath)
    
    
            UICollectionAttributes?.append(newItem!)
    
        }
    
        return UICollectionAttributes
    }
    
    override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
    {
        let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, with: indexPath)
    
        attributes.size = CGSize(width: self.collectionView!.bounds.size.width, height: 75) //your footer size
    
        if elementKind.isEqualToString(find: UICollectionElementKindSectionFooter)
        {
            updateFooter(attributes: attributes)
        }
        return attributes
    }
    
    func updateFooter(attributes : UICollectionViewLayoutAttributes){
        let currentBounds = self.collectionView?.bounds
        attributes.zIndex = 1024
        attributes.isHidden = false
        let yOffset = currentBounds!.origin.y + currentBounds!.size.height - attributes.size.height/2.0
        attributes.center = CGPoint(x: currentBounds!.midX, y: yOffset)
    
    }}
    

    【讨论】:

    • 将 StickyFooter 类分配给您的 collectionviewflow 布局
    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 2015-05-18
    • 2020-03-24
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2017-03-23
    相关资源
    最近更新 更多