How to provide different sizes for iMessage Sticker App Grid Sticker

I created the iOS iMessage sticker app, and according to Apple's documentation, I can display stickers in one of three different sizes.

However, no matter what size I make sticker images (300 by 300 pixels, 408 by 408 pixels, 618 by 618 pixels), they are displayed only as a medium grid, with three labels per line. Does anyone know how to solve this problem, maybe I'm missing something simple?

There is little documentation on this topic because it is fairly new. Thanks for the help.

Sticker Sizing Chart

Documentation link: https://developer.apple.com/ios/human-interface-guidelines/extensions/messaging/

+2
1

300x300, 408x408 618x618 , ( . )..

StickerBrowserView, :

  • InterfaceBuilder

StickerPack, ( ), " " : (2 ), (3 ) (4 ). ! . , , / .

Change Sticker Size from Interface Builder

" ", MSStickerBrowserViewController, , / ( ) , 2 . MSStickerBrowserViewController. .

SubclassFile.Swift

import UIKit
import Messages

class MyBrowserVC: MSStickerBrowserViewController {

    //create stickers array
    var stickers = [MSSticker]()

    //load assets into stickers array
    func loadStickers() {
        createSticker(asset: "boycott", localizedDescription: "boycottSticker")
        createSticker(asset: "alluminaughty", localizedDescription: "alluminaughtySticker")
        createSticker(asset: "beer", localizedDescription: "beerSticker")
    }

    //func to create sticker
    func createSticker(asset: String, localizedDescription: String) {

        //create url from assets in main bundle
        guard let stickerPath = Bundle.main.path(forResource: asset, ofType: "png") else {
            print("Couldn't create sticker path for", asset)
            return
        }
        let stickerURL = URL(fileURLWithPath: stickerPath)
        let sticker: MSSticker

        //create sticker from path(and localized description) and add to array
        do {
            try sticker = MSSticker(contentsOfFileURL: stickerURL,
                                 localizedDescription: localizedDescription)
            stickers.append(sticker)
        } catch {
            print(error)
            return
        }
    }

    //datasource methods
    override func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
        return stickers.count
    }
    override func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView,
                                          stickerAt index: Int) -> MSSticker {
        return stickers[index]
    }
}

MessagesViewController ( ) BrowserVC , , browserVC, .

MessageViewController.swift

import UIKit
import Messages

class MessagesViewController: MSMessagesAppViewController {

    //create BrowserVC instance of Subclass
    var browserVC: MyBrowserVC!

    override func viewDidLoad() {
        super.viewDidLoad()

    //the next line of code is where you can adjust the "grid layout"
    //your options are: .small(2 columns), .regular(3 columns), or .large(4 columns)

        //instantiate browserVC with sticker size and set frame
        browserVC = MyBrowserVC(stickerSize: .regular)
        browserVC.view.frame = self.view.frame

        //send browserVC to front
        self.addChild(browserVC)
        browserVC.didMove(toParent: self)
        self.view.addSubview(browserVC.view)

        //load stickers onto the browser view
        browserVC.loadStickers()
        browserVC.stickerBrowserView.reloadData()
    }
}

WWDC, ! , !

+2

Source: https://habr.com/ru/post/1655666/


All Articles