Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS and disabling keyboard search or enter key

Tags:

ios

keyboard

Is there any way to disable the keyboard's search or enter buttons in iOS?

I'd like to disable these while the text in my UISearchBar is too short to commit a search.

like image 327
SK9 Avatar asked Jun 13 '11 05:06

SK9


People also ask

How do I get my iPhone keyboard back to normal?

Go to Settings > Accessibility > Keyboards, tap Full Keyboard Access, then turn on Full Keyboard Access.

How do I get rid of the keyboard after typing on iPhone?

Remove a keyboard: Tap Edit, tap. next to the keyboard you want to remove, tap Delete, then tap Done. Reorder your keyboard list: Tap Edit, drag.


3 Answers

It's worth noting that you can change enablesReturnKeyAutomatically on the text field, this will cause the return/done key to be disabled when there is no text in the text field.

// default is NO (when YES, will automatically disable return key when text widget has zero-length contents, and will automatically enable when text widget has non-zero-length contents)

However as SK9 has I'm sure noticed you can't specify rules for when it should be enabled and disabled.

like image 160
robmcm Avatar answered Sep 21 '22 15:09

robmcm


I can't find any class named UISearchView - did you mean UISearchBar? If so, keep reading...

You can't disable (gray out) the button programmatically. Implement the searchBar:shouldChangeTextInRange:replacementText: method on the UISearchBarDelegate to check for the replacement text @"\n" (thats the return key press) and if they have entered enough text to allow it to search, and do nothing if they haven't.

If you really want to disable the return key, you can build your own keyboard view (with no framework keyboard to inherit from to get existing functionality) and return it from the inputView property of a UITextView (which means building your own search view as well)

like image 43
RyanR Avatar answered Sep 21 '22 15:09

RyanR


There is a very simple solution to this with no UI hacks, no accessing Apple private apis and such. Simply add this to viewDidLoad.

        yourSearchBar.enablesReturnKeyAutomatically = true

As per Apples documentation:

If you set it to true, the keyboard disables the Return key when the text entry area contains no text. As soon as the user enters some text, the Return key is automatically enabled.

like image 23
user7097242 Avatar answered Sep 23 '22 15:09

user7097242