0

I am writing a custom keyboard with Swift. I am facing some problems in making return button. The return button should change title "Return", "Done" and "Search" according to textField that user is writing.

To do it, I write like this.

// Syntax are not correct. I just want to explain my idea.

override fun viewDidLoad()
{
super.viewDidLoad()
if ((textDocumentProxy as UIKeyInput).returnKeyType == .Default)
{
btnReturn.addTarget("addParagraph", .TouchUpInside)
btnReturn.setTitleLabel("Return", .Normal)
}
else if ((textDocumentProxy as UIKeyInput).returnKeyType == .Done)
{
btnReturn.addTarget("DoneKey", .TouchUpInside)
btnReturn.setTitleLabel("Done", .Normal)
}
else if ((textDocumentProxy as UIKeyInput).returnKeyType == .Search)
{
btnReturn.addTarget("searchKey", .TouchUpInside)
btnReturn.setTitleLabel("Search", .Normal)
}

Am I doing this correctly?

I can do the button to add paragraph but I don't know how to hide the keyboard when the button title is "Done" and don't know how to make search function when the button title is "Search".

Anyone please help me.

thura oo
  • 493
  • 1
  • 6
  • 18

3 Answers3

1

You don't need to provide this functionality since you are just building a keyboard.

It is the responsibility of the app to provide a search function or to dismiss the keyboard whenever the return key is tapped.

Duc
  • 650
  • 5
  • 10
0

for hiding keyboard:

func hideKeyboard() {
    self.view.endEditing(true)
}
  • I can't do. I think self.view.endEditing() does not work for custom keyboards. However, thanks for your help. – thura oo Dec 13 '15 at 09:43
  • This dismisses the active keyboard from a ViewController containing a DocumentProxy, it won't dismiss the keyboard from within its own ViewController – froggomad Sep 16 '19 at 21:01
0

Use the builtin method dismissKeyboard() to hide the keyboard from within the keyboard's ViewController

Swift 4:

self.dismissKeyboard()
Community
  • 1
  • 1
froggomad
  • 1,747
  • 2
  • 17
  • 40