Bram Roelandts
Bram Roelandts

Reputation: 470

Custom Key on Keyboard Swift

In my app, I'm using a search function which can search words or numbers. I need two different keyboards, the decimal and the regular one. At the moment, the user is able to switch between those two in settings of the app.

I would like to add a key on both keyboards which allows the user to switch between number search and word search, but I wasn't able to figure out how. Introduced in iOS 8, you can create custom keyboards but isn't this something the user has to enable in the iPhone settings and can use in every app?

Am I able to design a custom keyboard and present it to the user only in my app, instead of the standard keyboard? Or am I able to add a button to the regular keyboard?

Thanks in advance, you're really helping me out!

Upvotes: 3

Views: 2632

Answers (1)

Vishnu gondlekar
Vishnu gondlekar

Reputation: 3956

You can attach your keyboard with toolbar which has two buttons for number search and word search. When buttons are pressed you can change the keyboard type. Checkout following code

func createToolbar(textField : UITextField) {
    let toolbar = UIToolbar()
    toolbar.barStyle = UIBarStyle.Default
    toolbar.sizeToFit()
    let numSearch = UIBarButtonItem(title: "Number", style: UIBarButtonItemStyle.Plain, target: self, action: "numberSearch")
    let wordSearch = UIBarButtonItem(title: "Word", style: UIBarButtonItemStyle.Plain, target: self, action: "wordSearch")
    toolbar.items = [numSearch, wordSearch]
    textField.inputAccessoryView = toolbar
}

func numberSearch() {
     //change keyboard type to number
}

func wordSearch() {
     //change keyboard type to default
}

Upvotes: 7

Related Questions