swift uicollection view black screen

Im new to Swift and I started learning collection view. Im trying to make design like in picture below

enter image description here

I implemented all required functions, but when I run my application I just get black screen. Also, how can I position collectionview so it start with two images and then continue normally?

My viewcontroller with collectionview:

import UIKit

class ResultViewController: UIViewController, UICollectionViewDelegate, 
UICollectionViewDataSource {

let images = [
    UIImage(named: "1"),
    UIImage(named: "2"),
    UIImage(named: "3"),
    UIImage(named: "4"),
    UIImage(named: "5"),
    UIImage(named: "6"),
    UIImage(named: "7"),
    UIImage(named: "8"),
    UIImage(named: "9"),
    UIImage(named: "10"),
    UIImage(named: "11"),
    UIImage(named: "12"),
    UIImage(named: "13"),
    UIImage(named: "14"),
    UIImage(named: "15"),
    UIImage(named: "16"),
    UIImage(named: "17"),
    UIImage(named: "18"),
    UIImage(named: "19"),
    UIImage(named: "20"),
    UIImage(named: "21"),
    UIImage(named: "22"),
    UIImage(named: "23"),
    UIImage(named: "24"),
    UIImage(named: "25"),
    UIImage(named: "26"),
    UIImage(named: "27"),
    UIImage(named: "28"),
    UIImage(named: "29"),
    UIImage(named: "30"),
    UIImage(named: "31"),
    UIImage(named: "32"),
]

let collectionView: UICollectionView = {
    
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    return collectionView
    
}()

override func viewDidLoad() {
    super.viewDidLoad()
    
    setupCollectionView()
}

func setupCollectionView(){
    view.addSubview(collectionView)
    collectionView.register(CardViewCell.self, forCellWithReuseIdentifier: CardViewCell.identifier)
    collectionView.dataSource = self
    collectionView.delegate = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CardViewCell.identifier, for: indexPath) as! CardViewCell
    let cellIndex = indexPath.item
    cell.cardImage.image = images[cellIndex]
    return cell
}
}

Cell design file:

import UIKit

class CardViewCell: UICollectionViewCell {

static let identifier = "CardViewCell"

let cardImage:UIImageView = {
    
    let image = UIImageView()
    return image
}()

let cardCount:UILabel = {
    
    let label = UILabel()
    label.addBadge(badgeCount: "3")
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
    
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    contentView.addSubview(cardImage)
    contentView.addSubview(cardCount)
    setupConstraints()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupConstraints(){
    
    NSLayoutConstraint.activate([
    
        cardCount.centerXAnchor.constraint(equalTo: cardImage.centerXAnchor),
        cardCount.centerYAnchor.constraint(equalTo: cardImage.centerYAnchor)
    ])
}
}
extension UILabel {
func addBadge(badgeCount: String) {
    self.text = badgeCount
    self.textColor = UIColor.orange
    self.textAlignment = .center
    self.font = UIFont.systemFont(ofSize: 20)
    self.layer.cornerRadius = 20
    self.layer.backgroundColor = UIColor.black.cgColor
    self.layer.masksToBounds = true
    
}
}


Read more here: https://stackoverflow.com/questions/67943385/swift-uicollection-view-black-screen

Content Attribution

This content was originally published by Sanja Kaloki at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: