UICollectionViewCompositionalLayout:在部分中右对齐单元格

时间:2020-05-05 12:24:24

标签: ios uicollectionviewcompositionallayout

我有这种布局,主要图片是UICollectionView,缩略图在自己的UICollectionView中。

**enter image description here**

设计要求缩略图右对齐,因此需要将UICollectionView中的内容推到右侧。我怎样才能做到这一点?我只需要支持iOS 13及更高版本,因此请使用UICollectionViewCompositionalLayout

private func createThumbsLayout() -> UICollectionViewLayout {
  let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
  let item = NSCollectionLayoutItem(layoutSize: itemSize)

  let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalHeight(1), heightDimension: .fractionalHeight(1))
  let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

  let section = NSCollectionLayoutSection(group: group)
  section.orthogonalScrollingBehavior = .continuous
  section.interGroupSpacing = 16
  section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)

  return UICollectionViewCompositionalLayout(section: section)
}

1 个答案:

答案 0 :(得分:0)

我发现了:

thumbsCollectionView.semanticContentAttribute = .forceRightToLeft

这是可行的,如果我然后还要反转图像并反转所有IndexPath数学运算以处理选择等问题。这不是理想的,所以会希望有更好的解决方案。当然必须有某种对齐方式吗?对于底部对齐的垂直collectionview,是否同样有用?