Swift

Create Custom Keyboard Just For Your App!

I’m building an iOS app using a Storyboard. And I had to add custom keyboard for my own app. For designing keyboard I had soo many difficulties(Autolayout is an art, not a science.) But finally I fixed. Then I looked everywhere to use keyboard like a subview. (I did not want to add keyboard like target but just subview.) Soo, Here is what I ended up doing to accomplish everything. The only thing you need to consider in addition to this is to think a basic subview and remove classic keyboard from UItextField.

1- So, I added -Content View- to my View Controller in StoryBoard and I added all buttons that I need for my own keyboard. Then in my ViewController class, to make it add buttons  to UITextField I wrote:

@IBAction func addBtnLabel(sender: AnyObject) {
         txtFieldNumber.text =  txtFieldNumber.text?.stringByAppendingString(sender.titleLabel!!.text!
 }

Then for my delete button I wrote:

@IBAction func deleteBtnLabel(sender: AnyObject) {

        

        if (txtFieldNumber == 0) {

            // textField is empty

            print("text field is empty")

        }

        else{

            //txtFieldNumber.text =  txtFieldNumber.text?.

            var name: String = txtFieldNumber.text!

            var truncated = String(name.characters.dropLast())

            txtFieldNumber.text = truncated

        }

    }

Also do NOT forget the removing keyboard! I wrote some sort of trick to remove standard keyboard in viewDidLoad:

  keyboardToPost.backgroundColor = UIColor(red: 98/255 , green: 102/255 , blue: 102/255 , alpha: 1.0)        

        // initialize custom keyboard

        let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

        txtFieldNumber.inputView = keyboardView

Thats what I ended up.
All files are in my github page with full version of it: https://github.com/rozeridilar/customKeyboard
If you have any questions, don’t hesitate to contact me!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s