Add Stickers Background Color With Swift

327 views Asked by At

I just made my first iMessage Stickers with Swift but I can't change the background color in the box where my stickers displays. I don't want it to be white. How can I do this? and where do I put the code?

I created a new Swift file under MessagesExtensions and used this code. I added all my images and connected them to the class in Storyboard. I have tried all type of codes that I have found online including this:

self.view.backgroundColor = UIColor.black  
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Pattern")!)  

I am a newborn to this and maybe I did not add the code in the correct place? I did not change the MessagesViewController.Swift code but am I supposed to?

Anyways, here is the code that I used:

    var stickers :[MSSticker]!

    var stickerNames = ["Brokkoli8","Carrot2","Eggplant4","Gresskar9","Lettuce6","Mushroom3","Onion10","Paprika2","Potato5","Tomato7"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.stickers = [MSSticker]()
        self.stickers = loadStickers()
    }

    override func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
        return self.stickers.count
    }

    override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {
        return self.stickers[index]
    }

    private func loadStickers() -> [MSSticker] {

        var stickers = [MSSticker]()

        for stickerName in self.stickerNames {

            guard let url = Bundle.main.url(forResource: stickerName,withExtension: "png") else {
                fatalError("resource does not exist")
            }

            let sticker = try! MSSticker (contentsOfFileURL: url,
                                          localizedDescription: stickerName)

            stickers.append(sticker)

        }

        return stickers
    }
}
1

There are 1 answers

0
Denys On

check out wwdc presentation on this: https://developer.apple.com/videos/play/wwdc2016/204/ particularly start on 20 min.

in short: you need to subclass MSStickerBrowserView on which you can set backgroundColor