Ask Your Question
4

How can a vertical list be incorporated into each horizontal cell on an orthogonal UICollectionView compositional layout using iOS nested scrolling?

asked 2023-05-16 02:36:42 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-16 02:50:01 +0000

pufferfish gravatar image

To incorporate a vertical list into each horizontal cell on an orthogonal UICollectionView compositional layout using iOS nested scrolling, follow these steps:

  1. 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.

  2. Implement the necessary UICollectionViewDelegate and UICollectionViewDataSource methods for the vertical UICollectionView inside the custom UICollectionViewCell class.

  3. 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")
  1. In the UICollectionViewDelegate's cellForItemAt method, dequeue and configure the custom cell class and return it:
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
}
  1. Set the vertical UICollectionView's height to be the height of the parent horizontal UICollectionView, and set the UICollectionView compositional layout item height to be the height of the horizontal UICollectionView. This will ensure that each horizontal cell contains a vertical list that fills the entire 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
  1. In the viewDidLoad method, enable the nested scrolling for both the horizontal and vertical UICollectionViews:
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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-16 02:36:42 +0000

Seen: 7 times

Last updated: May 16 '23