左对齐集合视图单元格没有单元格宽度超过内容视图?

时间:2017-01-20 23:08:44

标签: ios uicollectionview uicollectionviewlayout

我有添加的单元格,当我添加宽度较大的单元格时,它会结束。我想要的是在单元格与内容视图右侧之间没有足够空间的情况下进行包装。

以下是发生的事情:

enter image description here

以下是我对集合视图的设置:

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewLeftAlignedLayout()
    layout.sectionInset = UIEdgeInsets(top: 15, left: self.tableView.separatorInset.left, bottom: 15, right: self.tableView.separatorInset.left)
    layout.minimumInteritemSpacing = 60
    layout.minimumLineSpacing = 20
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .black
    cv.isScrollEnabled = false
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.backgroundColor = .clear

    cv.dataSource = self
    cv.delegate = self
    cv.register(TagCollectionViewCell.self, forCellWithReuseIdentifier: "tagCV")
    return cv
}()

我还有一些代码可以通过我从GitHub获得的单元格属性来对齐单元格 (https://github.com/fanpyi/UICollectionViewLeftAlignedLayout-Swift):

extension UICollectionViewLayoutAttributes {
    func leftAlignFrameWithSectionInset(_ sectionInset:UIEdgeInsets){
        var frame = self.frame
        frame.origin.x = sectionInset.left
        self.frame = frame
    }
}

class UICollectionViewLeftAlignedLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

    var attributesCopy: [UICollectionViewLayoutAttributes] = []
    if let attributes = super.layoutAttributesForElements(in: rect) {
        attributes.forEach({ attributesCopy.append($0.copy() as! UICollectionViewLayoutAttributes) })
    }

    for attributes in attributesCopy {
        if attributes.representedElementKind == nil {
            let indexpath = attributes.indexPath
            if let attr = layoutAttributesForItem(at: indexpath) {
                attributes.frame = attr.frame
            }
        }
    }
    return attributesCopy
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

    if let currentItemAttributes = super.layoutAttributesForItem(at: indexPath as IndexPath)?.copy() as? UICollectionViewLayoutAttributes {
        let sectionInset = self.evaluatedSectionInsetForItem(at: indexPath.section)
        let isFirstItemInSection = indexPath.item == 0
        let layoutWidth = self.collectionView!.frame.width - sectionInset.left - sectionInset.right

        if (isFirstItemInSection) {
            currentItemAttributes.leftAlignFrameWithSectionInset(sectionInset)
            return currentItemAttributes
        }

        let previousIndexPath = IndexPath.init(row: indexPath.item - 1, section: indexPath.section)

        let previousFrame = layoutAttributesForItem(at: previousIndexPath)?.frame ?? CGRect.zero
        let previousFrameRightPoint = previousFrame.origin.x + previousFrame.width
        let currentFrame = currentItemAttributes.frame
        let strecthedCurrentFrame = CGRect.init(x: sectionInset.left,
                                                y: currentFrame.origin.y,
                                                width: layoutWidth,
                                                height: currentFrame.size.height)
        // if the current frame, once left aligned to the left and stretched to the full collection view
        // widht intersects the previous frame then they are on the same line
        let isFirstItemInRow = !previousFrame.intersects(strecthedCurrentFrame)

        if (isFirstItemInRow) {
            // make sure the first item on a line is left aligned
            currentItemAttributes.leftAlignFrameWithSectionInset(sectionInset)
            return currentItemAttributes
        }

        var frame = currentItemAttributes.frame
        frame.origin.x = previousFrameRightPoint + evaluatedMinimumInteritemSpacing(at: indexPath.section)
        currentItemAttributes.frame = frame
        return currentItemAttributes

    }
    return nil
}

func evaluatedMinimumInteritemSpacing(at sectionIndex:Int) -> CGFloat {
    if let delegate = self.collectionView?.delegate as? UICollectionViewDelegateFlowLayout {
        let inteitemSpacing = delegate.collectionView?(self.collectionView!, layout: self, minimumInteritemSpacingForSectionAt: sectionIndex)
        if let inteitemSpacing = inteitemSpacing {
            return inteitemSpacing
        }
    }
    return self.minimumInteritemSpacing

}

func evaluatedSectionInsetForItem(at index: Int) ->UIEdgeInsets {
    if let delegate = self.collectionView?.delegate as? UICollectionViewDelegateFlowLayout {
        let insetForSection = delegate.collectionView?(self.collectionView!, layout: self, insetForSectionAt: index)
        if let insetForSectionAt = insetForSection {
            return insetForSectionAt
        }
    }
    return self.sectionInset
    }
}

0 个答案:

没有答案