wm.p1us wm.p1us - 4 months ago 29
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() {
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

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