'Swift CollectionView items do not scroll

my CollectionView's items do not scroll, when I scroll down the first items stay at their place, without any change, you can see what I mean on the image: collectionview Here is the code of the collectionView:

func setUpCollectionView() {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
                    layout.sectionInset = UIEdgeInsets(top: 20, left: 15, bottom: 20, right: 15)
                    layout.itemSize = CGSize(width: 100, height: 100)
        
                    myCollectionView = UICollectionView(frame: self.contentView.frame, collectionViewLayout: layout)
                    myCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
                    myCollectionView?.backgroundColor = UIColor.clear
                    myCollectionView?.allowsSelection = true
        
                    myCollectionView?.dataSource = self
                    myCollectionView?.delegate = self
        
                    contentView.addSubview(myCollectionView ?? UICollectionView())
        myCollectionView?.register(SpendingCell.self, forCellWithReuseIdentifier: "Spending")
        
        myCollectionView?.translatesAutoresizingMaskIntoConstraints = false
        myCollectionView?.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
        myCollectionView?.widthAnchor.constraint(equalTo: contentView.widthAnchor).isActive = true
        myCollectionView?.heightAnchor.constraint(equalToConstant: 350).isActive = true
    }

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Spending", for: indexPath) as! SpendingCell
        cell.setUpSpending(cost: categories[indexPath.row])
        return cell
        
    }
        
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 23
        }

and the collectionView cell:

class SpendingCell: UICollectionViewCell {
    
    let stackView = UIStackView()
    let SpendingButton = UIImageView()
    let nameLabel = UILabel()
    let sumLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(stackView)

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpSpending(cost: Spending) {
        stackView.alignment = .center
        stackView.axis = .vertical
        stackView.distribution = .fillProportionally
        stackView.spacing = 5
        
        stackView.addArrangedSubview(SpendingButton)
        stackView.addArrangedSubview(nameLabel)
        
        //Constraints
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        stackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        stackView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        stackView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        
        SpendingButton.image = cost.image
        SpendingButton.contentMode = .scaleAspectFit
        SpendingButton.layer.cornerRadius = 40
        SpendingButton.layer.masksToBounds = true
        
        SpendingButton.translatesAutoresizingMaskIntoConstraints = false
        SpendingButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
        SpendingButton.heightAnchor.constraint(equalToConstant: 80).isActive = true
        
        
        nameLabel.text = cost.title
        nameLabel.textAlignment = .center
        nameLabel.textColor = .black
        nameLabel.font = UIFont.boldSystemFont(ofSize: 10)
        
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
        nameLabel.heightAnchor.constraint(equalToConstant: 15).isActive = true
    }
    
    
}

Could someone please help me how to change that? Thanks in advance!



Solution 1:[1]

func makeKeyboardCollectionView() {
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 70, height: 40)
    layout.scrollDirection = .vertical
    keyboardCollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    contentView.addSubview(keyboardCollectionView)
    
    keyboardCollectionView.snp.makeConstraints({ make in
        make.top.equalTo(vocabularyLabel.snp.bottom).inset(-5)
        make.left.equalToSuperview().inset(40)
        make.right.equalToSuperview().inset(50)
        make.height.equalTo(210)
    })
    keyboardCollectionView.backgroundColor = .clear
    keyboardCollectionView.showsVerticalScrollIndicator = false
    keyboardCollectionView.register(KeyboardCollectionViewCell.self, forCellWithReuseIdentifier: KeyboardCollectionViewCell.identifier)
    keyboardCollectionView.dataSource = self
    keyboardCollectionView.delegate = self
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Venus