Creating Sticker Pack programmatically, issue with casting UICollectionViewCell as MSStickerView
Asked Answered
A

1

8

So, I have opted to using no storyboards in my messages extension sticker pack. The iMessage app comes with a storyboard file and a MessagesViewController.swift. I created 2 files named CollectionViewController and StickerCell. The idea is to subclass CollectionViewCell and cast it as MSStickerView and dequeue that view into my CollectionView as a "cell".

Here is the code for setting up the "StickerCell" as MSSstickerView:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let item = data[indexPath.row]
    return deQStickerCell(for: item, at: indexPath)
}

private func deQStickerCell(for sticker: MSSticker, at indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! StickerCell
    cell.stickerView.sticker = sticker
    return cell
}

and the code in my StickerCell class:

class StickerCell: UICollectionViewCell {
    var stickerView: MSStickerView!
}

I'm guessing the issue is here as I have done this successfully with storyboard and the exact code in StickerClass, except the var was an IBOutlet. So clearly something is not connecting to the CollectionView or I missed a step. In Interface Builder, I would create the CollectionViewController, give it a CollectionView, give it a CollectionViewCell, then slap a UIView on top and change it's class to MSStickerView.

How do I recreate the Interface Builder workflow programmatically!?

Aldis answered 3/11, 2017 at 0:5 Comment(0)
R
2

It's hard to know exactly what your issue is, but from what I can tell you aren't getting back a StickerCell with a subview of stickerView. What stands out different from IB is that you aren't ever initializing your stickerView. You need to add an init that will create and add the view to your cell. Something like (pseudo code):

override init(frame: CGRect) {
    super.init(frame: frame)

    self.stickerView = StickerView()
    self.contentView.addSubview(self.stickerView)
    // Set up your constraints & layout
}
Recidivism answered 6/11, 2017 at 23:34 Comment(1)
Ohhhhh totally. I take for granted what IB does for me. Which is why I'm forcing myself to do things programmatically. I'll give something like this a shot.Aldis

© 2022 - 2024 — McMap. All rights reserved.