添加贴纸背景颜色与Swift

时间:2017-12-16 20:34:56

标签: ios swift uicolor imessage ios-stickers

我刚刚使用Swift制作了我的第一个iMessage贴纸,但我无法更改贴纸显示的框中的背景颜色。我不希望它变成白色。我怎样才能做到这一点?我在哪里放代码?

我在MessagesExtensions下创建了一个新的Swift文件并使用了此代码。我添加了所有图像,并将它们连接到Storyboard中的班级。我尝试过在网上找到的所有类型的代码,包括:

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

我是新生儿,也许我没有在正确的地方添加代码?  我没有更改MessagesViewController.Swift代码,但我应该这样做吗?

无论如何,这是我使用的代码:

    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 个答案:

答案 0 :(得分:1)

查看wwdc演示文稿:https://developer.apple.com/videos/play/wwdc2016/204/ 特别是20分钟开始。

简而言之:你需要子类化MSStickerBrowserView,你可以在其上设置backgroundColor