swift集合视图可扩展标签单元格约束bug

时间:2017-11-21 18:23:45

标签: ios iphone swift uicollectionview

如果它是一个长文本我有这个约束错误

这是错误图片

enter image description here

我刚刚发现代码并且执行相同但我无法完全理解

flowlayout代码

class TagFlowLayout: UICollectionViewFlowLayout {
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let attributesForElementsInRect = super.layoutAttributesForElements(in: rect)
        var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()

        var leftMargin: CGFloat = 0.0;

        for attributes in attributesForElementsInRect! {
            if (attributes.frame.origin.x == self.sectionInset.left) {
                leftMargin = self.sectionInset.left
            } else {
                var newLeftAlignedFrame = attributes.frame
                newLeftAlignedFrame.origin.x = leftMargin
                attributes.frame = newLeftAlignedFrame
            }
            leftMargin += attributes.frame.size.width + 8
            newAttributesForElementsInRect.append(attributes)
        }

        return newAttributesForElementsInRect
    }
}

TAG CELL CODE

class TagCell: UICollectionViewCell {
    @IBOutlet var tagLabel: UILabel!
    @IBOutlet var maxConstraint: NSLayoutConstraint!
    @IBOutlet var tagView: UIView!
    @IBOutlet var photo: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.tagLabel.textColor = UIColor.white
        self.backgroundColor = UIColor.clear
        self.layer.cornerRadius = 22.0
        self.layer.masksToBounds = true
        self.layer.borderWidth = 2.0
        self.layer.borderColor = UIColor.white.cgColor
        self.maxConstraint.constant = UIScreen.main.bounds.width - 8 * 2 - 8 * 2
    }

}

在主流视图中使用此流程布局

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        self.configureCell(cell: self.sizingCell!, forIndexPath: indexPath)
        return self.sizingCell!.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
    }

这是xib文件

enter image description here

我已经尝试了很多时间来解决这个问题我无法做到这一点请帮我找到解决方案:(

0 个答案:

没有答案