How to change UICollectionView Scroll Direction?

前端 未结 2 1068
我在风中等你
我在风中等你 2021-02-01 02:20

I have successfully implemented a UICollectionView. Is it possible to change the scrollDirection?

Can you please show how to implement it pro

相关标签:
2条回答
  • 2021-02-01 02:47

    This is how you create it in code with UICollectionViewFlowLayout

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
    

    or if you are working with an existent collection view

    if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        layout.scrollDirection = .vertical
    }
    
    0 讨论(0)
  • 2021-02-01 02:59

    if you did not use storyboard and all is done with programmatically. so when we do that like this:

      lazy var snapCollectionView: UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            let collectionView = UICollectionView(frame: CGRect.zero, 
            collectionViewLayout: layout)
            layout.scrollDirection = .horizontal
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.backgroundColor = UIColor(named: "background")
            return collectionView
        }()
    
    0 讨论(0)
提交回复
热议问题