Advertisement
Don_Mag

Untitled

May 30th, 2023 (edited)
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.34 KB | None | 0 0
  1. class MyCell: UICollectionViewCell {
  2.     static let reuseIdentifier: String = "c"
  3. }
  4.  
  5. class FullScreenCollVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
  6.    
  7.     var pageCollectionView: UICollectionView!
  8.    
  9.     let colors: [UIColor] = [
  10.         .cyan, .yellow, .systemGreen, .systemBlue,
  11.     ]
  12.    
  13.     override func viewDidLoad() {
  14.         super.viewDidLoad()
  15.         view.backgroundColor = .red
  16.         setupPageCollectionView()
  17.     }
  18.     private func setupPageCollectionView() {
  19.        
  20.         let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  21.         flowLayout.scrollDirection = .horizontal
  22.         flowLayout.minimumInteritemSpacing = 0
  23.         flowLayout.minimumLineSpacing = 0
  24.         flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  25.        
  26.         self.view.backgroundColor = .red
  27.        
  28.         pageCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
  29.         pageCollectionView.register(MyCell.self, forCellWithReuseIdentifier: MyCell.reuseIdentifier)
  30.         pageCollectionView.delegate = self
  31.         pageCollectionView.dataSource = self
  32.         pageCollectionView.decelerationRate = .fast
  33.         pageCollectionView.translatesAutoresizingMaskIntoConstraints = false
  34.        
  35.         pageCollectionView.backgroundColor = .blue
  36.         self.view.addSubview(pageCollectionView)
  37.         NSLayoutConstraint.activate([
  38.             pageCollectionView.topAnchor.constraint(equalTo: self.view.topAnchor),
  39.             pageCollectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
  40.             pageCollectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
  41.             pageCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
  42.         ])
  43.        
  44.     }
  45.     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  46.         print(indexPath.item, collectionView.bounds.size)
  47.         return collectionView.bounds.size
  48.     }
  49.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  50.         return 5
  51.     }
  52.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  53.         let c = collectionView.dequeueReusableCell(withReuseIdentifier: MyCell.reuseIdentifier, for: indexPath) as! MyCell
  54.         c.contentView.backgroundColor = colors[indexPath.item % colors.count]
  55.         return c
  56.     }
  57.    
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement