Yumin Hwang
Yumin Hwang

Reputation: 159

How to make custom keyboard work with Swift

i'm making custom keyboard using swift. the thing i want is that if i key 'a', 'a' can appear on the search bar(safari search bar or somewhere)

i combined all alphabet keys to @IBAction fun didTapKey.

@IBOutlet var key is that setting shadow for key buttons

this is the part of keyboardViewController.swift. (pls ignore nextkeyboarbutton, this is not the one i created( i think this is kinda default button ;-; )

class KeyboardViewController: UIInputViewController {
var newKeyboardView: UIView!

@IBAction func didTapKey(sender: UIButton) {

}

@IBOutlet var key: [UIButton]!


@IBOutlet var nextKeyboardButton: UIButton!

override func updateViewConstraints() {
    super.updateViewConstraints()

    // Add custom view sizing constraints here
}

override func viewDidLoad() {
    super.viewDidLoad()

    loadInterface()

    for button in self.key {
        button.layer.masksToBounds = false
        button.layer.shadowOffset = CGSizeMake(0.0, 2.0)
        button.layer.cornerRadius = 4.0
        button.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).CGColor
        button.layer.shadowOpacity = 1.0
        button.layer.shadowRadius = 0.0
    }

Upvotes: 1

Views: 236

Answers (1)

Max Pevsner
Max Pevsner

Reputation: 4104

When you get the tapped key letter you pass it to the text field the keyboard is currently focused on. The text field you get by:

let textDocumentProxy = self.textDocumentProxy as UIKeyInput

and then you insert your String to that proxy:

textDocumentProxy.insertText(myString)

Upvotes: 1

Related Questions