我刚刚iMessage
用 Swift 制作了我的第一个贴纸,但我无法更改贴纸显示框中的背景颜色。我不希望它是白色的。我怎样才能做到这一点?我把代码放在哪里?
我在下面创建了一个新的 Swift 文件MessagesExtensions
并使用了这段代码。我添加了所有图像并将它们连接到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
}
}