To incorporate a vertical list into each horizontal cell on an orthogonal UICollectionView compositional layout using iOS nested scrolling, follow these steps:
Create a custom UICollectionViewCell that will contain the vertical UICollectionView. You can let the cell conform to the UICollectionViewDelegate and UICollectionViewDataSource protocols, and add a vertical UICollectionView to it in the cell's init method.
Implement the necessary UICollectionViewDelegate and UICollectionViewDataSource methods for the vertical UICollectionView inside the custom UICollectionViewCell class.
Add the custom UICollectionViewCell to the UICollectionView compositional layout section that you want to contain the vertical list. You can do this by registering the custom cell class:
collectionView.register(MyCustomCollectionViewCell.self, forCellWithReuseIdentifier: "CustomCell")
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! MyCustomCollectionViewCell
// Configure the cell with the data for the vertical UICollectionView
// ...
return cell
}
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(collectionView.bounds.height))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(collectionView.bounds.height))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0)
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .groupPaging
collectionView.isDirectionalLockEnabled = true // if needed
collectionView.alwaysBounceVertical = true
collectionView.alwaysBounceHorizontal = true
collectionView.isPagingEnabled = true // if using groupPaging orthogonalScrollingBehavior
for scrollView in collectionView.subviews {
if let verticalScrollView = scrollView as? UICollectionView {
verticalScrollView.alwaysBounceHorizontal = false
}
}
These steps should result in a horizontal UICollectionView layout with a vertical list inside each cell that scrolls both horizontally and vertically.
Asked: 2023-05-16 02:36:42 +0000
Seen: 7 times
Last updated: May 16 '23