【问题标题】:get the actual UICollectionView item spacing获取实际的 UICollectionView 项目间距
【发布时间】:2016-08-04 13:28:34
【问题描述】:

如何在 UICollectionView 中获得水平流布局的实际 interItemSpacing ?我能得到(和设置)的唯一信息是minimumInteritemSpacing

为了以下小计算,我需要它来始终在集合视图中显示 3 个等宽项,而不管屏幕大小(我想用公式中的 interItemSpacing 的当前值替换 minimumInteritemSpacing): flowLayout.itemSize = CGSizeMake((_collectionView.frame.size.width/3.0)-(flowLayout.minimumInteritemSpacing/3.0), _collectionView.frame.size.height); 我把这个计算放在viewDidLayoutSubviews 中,但它不适用于某些屏幕,因为它们当前的interItemSpacingminimumInteritemSpacing 不同

提前致谢。

【问题讨论】:

  • 如果您没有使用UICollectionViewFlowlayoutDelegate method 允许动态调整大小的单元格的原因 - 将您的计算放在那里并返回您想要的大小?
  • 忽略以上 - 有人将一个 2 年前的问题拖到了热门问题中......

标签: ios iphone uicollectionview cellspacing


【解决方案1】:

您可以通过比较两个连续单元格来获得实际间距

类似

let firstIndex =  IndexPath(item: 0, section: 0)
let secondIndex = IndexPath(item: 1, section: 0)
if let att1 = collectionView.layoutAttributesForItem(at: firstIndex),
let att2 = collectionView.layoutAttributesForItem(at: secondIndex) {
  let actualSpace = att2.frame.minX - att1.frame.maxX
  print (actualSpace)
}

【讨论】:

    【解决方案2】:

    要创建具有固定间距的单元格,请尝试以下操作:

    private let minItemSpacing: CGFloat = 8
    private let itemWidth: CGFloat      = 100
    private let headerHeight: CGFloat   = 32
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        // Create our custom flow layout that evenly space out the items, and have them in the center
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        layout.minimumInteritemSpacing = minItemSpacing
        layout.minimumLineSpacing = minItemSpacing
        layout.headerReferenceSize = CGSize(width: 0, height: headerHeight)
    
        // Find n, where n is the number of item that can fit into the collection view
        var n: CGFloat = 1
        let containerWidth = collectionView.bounds.width
        while true {
            let nextN = n + 1
            let totalWidth = (nextN*itemWidth) + (nextN-1)*minItemSpacing
            if totalWidth > containerWidth {
                break
            } else {
                n = nextN
            }
        }
    
        // Calculate the section inset for left and right. 
        // Setting this section inset will manipulate the items such that they will all be aligned horizontally center.
        let inset = max(minItemSpacing, floor( (containerWidth - (n*itemWidth) - (n-1)*minItemSpacing) / 2 ) )
        layout.sectionInset = UIEdgeInsets(top: minItemSpacing, left: inset, bottom:    minItemSpacing, right: inset)
    
        collectionView.collectionViewLayout = layout
    }
    

    更多信息,这篇优秀的文章:

    http://samwize.com/2015/11/30/understanding-uicollection-flow-layout/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      相关资源
      最近更新 更多