Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Sticker Pack programmatically, issue with casting UICollectionViewCell as MSStickerView

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!?

like image 482
Jake Dobson Avatar asked Nov 03 '17 00:11

Jake Dobson


1 Answers

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
}
like image 81
stevethomp Avatar answered Nov 01 '22 03:11

stevethomp