簡體   English   中英

如何在單行集合視圖中顯示選定的照片

[英]How to display selected photos in a single row collectionview

如何顯示從uiimagepickercontroller中選擇的圖像,如顯示的圖像。 我在collectionview上出現黑色區域​​。 我什至不確定使用水平uicollectionview是最好的方法。

例

我認為使用水平UICollectionView是負擔得起的想法。 如果將其添加為頁腳,則可以使“添加圖片按鈕”始終位於最后。

為此,請對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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM