Advertisement
Don_Mag

Horizontal Collection View - auto-scroll on load

Apr 8th, 2024 (edited)
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.67 KB | None | 0 0
  1. class SingleLabelCell: UICollectionViewCell {
  2.     static let identifier: String = "singleLabelCell"
  3.    
  4.     let theLabel: UILabel = {
  5.         let v = UILabel()
  6.         v.font = .systemFont(ofSize: 14.0, weight: .light)
  7.         v.textAlignment = .center
  8.         v.numberOfLines = 0
  9.         v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
  10.         v.layer.cornerRadius = 12
  11.         v.clipsToBounds = true
  12.         v.translatesAutoresizingMaskIntoConstraints = false
  13.         return v
  14.     }()
  15.     override init(frame: CGRect) {
  16.         super.init(frame: frame)
  17.         commonInit()
  18.     }
  19.     required init?(coder: NSCoder) {
  20.         super.init(coder: coder)
  21.         commonInit()
  22.     }
  23.     func commonInit() -> Void {
  24.         contentView.addSubview(theLabel)
  25.         let g = contentView
  26.         NSLayoutConstraint.activate([
  27.             theLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 4.0),
  28.             theLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 4.0),
  29.             theLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -4.0),
  30.             theLabel.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -4.0),
  31.         ])
  32.        
  33.         // during development, so we can see the cell framing
  34.         contentView.backgroundColor = .systemYellow
  35.     }
  36. }
  37.  
  38. class SampleViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  39.    
  40.     var collectionCarousel: UICollectionView!
  41.    
  42.     override func viewDidLoad() {
  43.         super.viewDidLoad()
  44.         view.backgroundColor = .systemBackground
  45.        
  46.         // horizontal flow layout
  47.         let fl = UICollectionViewFlowLayout()
  48.         fl.scrollDirection = .horizontal
  49.         fl.minimumLineSpacing = 8
  50.         fl.minimumInteritemSpacing = 8
  51.         fl.itemSize = .init(width: 80.0, height: 40.0)
  52.         collectionCarousel = UICollectionView(frame: .zero, collectionViewLayout: fl)
  53.        
  54.         collectionCarousel.translatesAutoresizingMaskIntoConstraints = false
  55.         view.addSubview(collectionCarousel)
  56.        
  57.         let g = view.safeAreaLayoutGuide
  58.         NSLayoutConstraint.activate([
  59.             // constrain collectionView to Top/Leading/Trailing of safe-area
  60.             collectionCarousel.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
  61.             collectionCarousel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
  62.             collectionCarousel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
  63.             collectionCarousel.heightAnchor.constraint(equalToConstant: 50.0),
  64.         ])
  65.        
  66.         collectionCarousel.register(SingleLabelCell.self, forCellWithReuseIdentifier: SingleLabelCell.identifier)
  67.         collectionCarousel.dataSource = self
  68.         collectionCarousel.delegate = self
  69.        
  70.         // so we can see the collection view framing
  71.         collectionCarousel.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
  72.        
  73.     }
  74.    
  75.     var bFirstLayout: Bool = true
  76.     override func viewDidLayoutSubviews() {
  77.         super.viewDidLayoutSubviews()
  78.        
  79.         // if this is the first layout of this controller,
  80.         //  center section 8 horizontally (but not animated)
  81.         if bFirstLayout {
  82.             bFirstLayout = false
  83.             selectItemAndScroll(8, animated: false)
  84.         }
  85.     }
  86.    
  87.     func numberOfSections(in collectionView: UICollectionView) -> Int {
  88.         return 11
  89.     }
  90.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  91.         return 1
  92.     }
  93.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  94.         let c = collectionView.dequeueReusableCell(withReuseIdentifier: SingleLabelCell.identifier, for: indexPath) as! SingleLabelCell
  95.         c.theLabel.text = "\(indexPath.section)"
  96.         return c
  97.     }
  98.    
  99.     func selectItemAndScroll(_ setIndex: Int = 0, animated: Bool ) {
  100.         let startIndexPath = IndexPath(item: 0, section: setIndex)
  101.         collectionCarousel.selectItem(at: startIndexPath,
  102.                                       animated: animated,
  103.                                       scrollPosition: .centeredHorizontally)
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement