wm.p1us wm.p1us - 10 days ago 5
Swift Question

Offset for content of the collection view cells

How to make content offset of collection view cell. Code below what do I have but It doesn't work for some reason:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "pCell", for: indexPath) as! CollectionViewCell
cell.imageView.image = UIImage(named: "1")!

cell.offset(offset: CGPoint(x: 0.0, y: -30.0))

return cell
}


My cell class code:

class CollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!
var image: UIImage = UIImage() {
didSet{
self.imageView.image = image
}
}

func offset(offset: CGPoint) {
self.imageView.frame = self.imageView.bounds.offsetBy(dx: offset.x, dy: offset.y)
}
}


I just can't see any offset when cells are displayed. What I do wrong, guys?

Wes Wes
Answer

Make sure that you don't have any IB Constraints enabled on imageView, they will keep you from updating the frame.

Comments