在集合视图中的水平页脚

时间:2014-05-27 11:37:01

标签: ios uicollectionview

我正在使用横​​向的CollectionView

enter image description here

我可以使用CollectionView轻松完成此操作,但标签为“6天连胜”,我认为该视图是一个页脚,但在水平CollectionView中,它显示在顶部或中间在肖像模式下的底部。

我想在这个ScrollView下创建一个CollectionView并同时移动两个,但我不知道这是不是一个可怕的想法。

我认为使用Flow Layout是不可能的,而且我不是自定义布局的专家,这是可行的吗?

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:0)

这是老问题,但我为其他有相同问题的人留下了解决方案。

您可以使页脚始​​终位于最后,将UICollectionViewFlowLayout子类化并覆盖layoutAttributesForElementsInRect()。

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    if let layoutAttributesForElementsInRect = super.layoutAttributesForElementsInRect(rect), let collectionView = self.collectionView {

      for layoutAttributes in layoutAttributesForElementsInRect {
            if layoutAttributes.representedElementKind == UICollectionElementKindSectionFooter {

                let section = layoutAttributes.indexPath.section
                let numberOfItemsInSection = collectionView.numberOfItemsInSection(section)

                if numberOfItemsInSection > 0 {
                    let lastCellIndexPath = NSIndexPath(forItem: max(0, numberOfItemsInSection - 1), inSection: section)

                    if let lastCellAttrs = self.layoutAttributesForItemAtIndexPath(lastCellIndexPath) {
                        var origin = layoutAttributes.frame.origin

                        origin.x = CGRectGetMaxX(lastCellAttrs.frame)

                        layoutAttributes.zIndex = 1024
                        layoutAttributes.frame.origin = origin
                        layoutAttributes.frame.size = layoutAttributes.frame.size
                    }
                }
            }
        }

        return layoutAttributesForElementsInRect
    }

    return nil
}