ios - 使用类似 "tag"的单元格设置 collectionView

标签 ios swift uicollectionview uicollectionviewflowlayout

我一直在使用自定义 UICollectionViewFlowLayout 来调整单元格之间的空间,以便在我的 collectionView 中获得良好的流动。但是使用我当前的代码,我无法弄清楚如何在不“破坏”行数的情况下调整单元格大小。这使得一些单元格认为它们可以容纳一行但实际上不能。

我的自定义流程布局

class UserProfileTagsFlowLayout: 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 // Makes the space between cells
            newAttributesForElementsInRect.append(attributes)
        }
        
        return newAttributesForElementsInRect
    }
}

所以在我的 ViewController 中我扩展了

extension myViewController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let tag = tagsArray[indexPath.row]
        let font = UIFont(name: "Raleway", size: 14)!
        let size = tag.value.size(withAttributes: [NSAttributedString.Key.font: font])
        let dynamicCellWidth = size.width

        /*
          The "+ 20" gives me the padding inside the cell
        */
        return CGSize(width: dynamicCellWidth + 20, height: 35)
    }
    
    // Space between rows
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    // Space between cells
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
}

当我尝试这段代码时,我在 iPhone X 的结果部分得到了结果。你可以看到标签“机器学习”跳到一个新行,因为它认为它可以适合该行多于。如果我在 sizeForItem 函数中删除单元格的“填充”,那么我就不会有这个问题。但它看起来很糟糕。

所以我的问题是:如何在不破坏 flowLayout 的情况下在我的单元格上使用填充?


目前我的结果是这样的:

iPhone X:

iPhone X result

最佳答案

也许它能帮到你。我认为您应该检查您的项目是否在右插图中突出了 Collection View 。 在 layoutAttributesForElements 方法中检查:

        let collectionViewWidth = self.collectionView?.frame.width - (self.sectionInset.right + self.sectionInset.left)

        if (attributes.frame.origin.x + attributes.frame.size.width > collectionViewWidth) {
            var newLeftAlignedFrame = attributes.frame
            newLeftAlignedFrame.origin.x = self.sectionInset.left
            attributes.frame = newLeftAlignedFrame 
        }

更新了我的答案,它对我有用,你可以在屏幕截图上看到它

关于ios - 使用类似 "tag"的单元格设置 collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52628403/

相关文章:

ios - 呈现 UICollectionView 时防止 UICollectionViewCell 动画外观

ios - 从 CollectionViewCell 中的 UIButton 打开 URL

ios - 应用内购买重定向到另一个 View Controller

ios - 如何为 iCarousel 提供可访问的滑动操作

ios - Swift 3 创建一个 NSNumber 对象

ios - FaSTLane 在运行 UI 测试时扫描 "Failed to synthesize event",在 Xcode 中运行

arrays - Swift 3,扩展数组,其中数组的项目属于某种类型

ios - 如何使用这样的图像/图标/ View 来偏移组合布局

ios - 可以在后台运行并可以连接到设备的IOS应用程序

objective-c - 警告 ASIHttpRequest