0

I am adding buttons programmatically to a collectionview. The images are all the same size (512px x 512px). I want the cell to be centered and it's size to be 250x250. I changed the size of the Collection View Cell to Custom and set the Width and Height in the inspector. In Collection Reusable View I have changed the Content Mode to Scale To Fill. My button has in the Viewsection the Content Mode also set to Scale To Fill. My Images are not scaled and look like in the screenshot. Thanks for help.

Here's the code where I add the Buttons with images:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionCell
        let appButton = cell.MyButton!
        cell.contentMode = .scaleAspectFit
        cell.clipsToBounds = true
        //appButton.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
        appButton.setTitle("", for: .normal)
        appButton.setImage(UIImage(named: webSeriesImage[indexPath.row]), for: .normal)
        appButton.contentMode = .scaleAspectFit
        appButton.clipsToBounds = true
        appButton.tag = indexPath.row
        //appButton.imageView?.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.3)
        appButton.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)
        collectionView.backgroundColor = .clear
        cell.contentMode = .redraw
        appButton.contentMode = .redraw
        return cell
    }

Screenshot with not scaled image

Edit:

I really tried a lot of things. Last thing I tried was:

cell.contentMode = .scaleAspectFit
cell.clipsToBounds = true

None of these worked for me.

Chris L
  • 87
  • 10

0 Answers0