I want cell's background to be completely filled with an image, but I am unable to do it.
I have tried all of image scalings, but none of them helped.
Code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
myCell.backgroundColor = .red
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: myCell.frame.width , height: myCell.frame.height))
let image = UIImage(named: "background")
imageView.image = image
imageView.clipsToBounds = true
myCell.backgroundView = UIView()
myCell.backgroundView!.addSubview(imageView)
return myCell
}
And this is how it looks like:
Maybe the problem is in my imageView's frame? I think its not because I tested with changing imageView's background colour and all the cell's red colour was overlayed with blue.
You will have to use constraints here so add this extension to a file in your project.
And then instead of
myCell.backgroundView!.addSubview(imageView)
Do this
myCell.backgroundView!.embedView(imageView)
You might want to read more about Autolayout n Constraints, so here are docs