Creating a custom Swift iOS 9 keyboard using CollectionView and CollectionViewCells

So, I kept looking for a guide on how to do this, and Im, striking with spaces, got scared!

So basically I want to create a simple emoji / image / gif keyboard extension for iOS using Swift.

For part of my main application, I have a viewController that contains a collectionView with a collection of ViewCell. Using the information from plist, I generate a collection of images (parent groups), and when selected, it drills to the second level, displaying all the images of this group. Selecting an image from this level copies the image to the clipboard.

So, I made this main part of the application absolutely beautiful, and now I want to simulate functionality like a keyboard.

So, I made the keyboard extension that created the file class KeyboardViewController: UIInputViewController. Then I created a new view file called Keyboard.xib. In the view, I added UICollectionView.

In KeyboardViewController, then call

override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: "Keyboard", bundle: nil)
    let objects = nib.instantiateWithOwner(self, options: nil)

    view = objects[0] as! UIView

    addKeyboardButtons()
}

That, when the application starts, it shows my empty CollectionView in the form of a keyboard.

Now my task is to populate the CollectionView with a reusable cell that will show the image from the names in the plist file.

In my main application, I was able to determine the number of elements in the collection that created the cell for me.

enter image description here

However, in the keyboard view, I do not have such an opportunity to add a cell. Dragging a cell into collectionView doesn't help either.

enter image description here

, , View Keyboard.xib, ?

- , xib, , imageView, xib collectionView.

xib, , , View resuable cell...

?

!!!

+4
1

, Keyboard.xib, .

class KeyboardView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: MyCollectionView!

    class func instanceFromNib() -> KeyboardView {
        return UINib(nibName: "Keyboard", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! KeyboardView
    }

    override func awakeFromNib() {
        print("awaking from nib")
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("adding numberOfItemsForSection")
        return 1
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        print("adding cell")
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor.greenColor()
        return cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
       print("setting size..")
       return CGSizeMake(320, 350)
    }
}

KeyboardViewController.swift, ,

override func viewDidLoad() {
    super.viewDidLoad()

    let keyboardView = KeyboardView.instanceFromNib()
    keyboardView.collectionView.registerNib(UINib.init(nibName: "Cell", bundle: nil), forCellWithReuseIdentifier: "myCell")
    self.view.addSubview(keyboardView)

    addKeyboardButtons()
}

!

+2

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


All Articles