Kevin Renskers
Kevin Renskers

Reputation: 5912

UICollectionViewCompositionalLayout: right align cells within a section

I have this layout, where the main picture is a UICollectionView, and the thumbnails are in their own UICollectionView.

**enter image description here**

The design asks for the thumbnails to be right-aligned, so the content in the UICollectionView needs to be pushed to the right. How can I do that? I only need to support iOS 13 and up, so am using 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)
}

Upvotes: 1

Views: 348

Answers (1)

Kevin Renskers
Kevin Renskers

Reputation: 5912

I found this:

thumbsCollectionView.semanticContentAttribute = .forceRightToLeft

Which works, if I then also reverse the images and reverse all IndexPath math to deal with selection and all that. It's not ideal so would love a better solution. Surely there must be some kind of alignment property? Same would be useful for a bottom-aligned vertical collectionview?

Upvotes: 1

Related Questions