Ios 使用UICollectionView和FlowLayout标记组件?

Ios 使用UICollectionView和FlowLayout标记组件?,ios,uicollectionview,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewlayout,我想做的是,如果没有空间,将每个视图放在下一行。与标记库使用的原理相同(请这样想)。我不希望他们滚动只是为了在下一行和左对齐。这是我的密码: self.collection.register(UINib(nibName: "FlowCell", bundle: nil), forCellWithReuseIdentifier: "tag") self.collection.showsHorizontalScrollIndicator = false self.collection

我想做的是,如果没有空间,将每个视图放在下一行。与标记库使用的原理相同(请这样想)。我不希望他们滚动只是为了在下一行和左对齐。这是我的密码:

self.collection.register(UINib(nibName: "FlowCell", bundle: nil), forCellWithReuseIdentifier: "tag")
    self.collection.showsHorizontalScrollIndicator = false
    self.collection.showsVerticalScrollIndicator = false
    self.collection.dataSource = self
    self.collection.delegate = self
    let layout = collection.collectionViewLayout as! UICollectionViewFlowLayout
    layout.estimatedItemSize = CGSize(width: 10, height: 18)
这就是结果

  • 具有水平滚动方向。这里我只有一行:
  • 具有垂直滚动方向。此处内容以中心为中心,其中:
  • 这就是我想要实现的目标:

    当然,最后一张图片上的项目内容不同,但如果标签不能在同一行中多个,我希望项目左对齐。

    您应该尝试以下方法:


    看起来正是您想要的。

    尝试各种不同的文本长度。它也在做同样的事情吗?