Zack Braksa
Zack Braksa

Reputation: 1666

Enable Return Key when Pasting into a UISearchController's UISearchBar

I'm running into a similar problem to the one described in a bug report to Apple.

Basically, when pasting text into a UISearchBar (part of UISearchController), the Return key is not enabled on the keyboard. (It gets enabled when typing characters though).

Essentially, enablesReturnKeyAutomatically is ignored, as this property should be true by default.

Steps to Reproduce:

Expected Results:

Actual Results:

Question

Even though this seems to be a bug, is there a workaround for this particular problem ? Especially that some apps like Twitter or Product Hunt got around it somehow.

Upvotes: 3

Views: 908

Answers (1)

Jay Ayres
Jay Ayres

Reputation: 74

The following will do it. Note that if pasting into a search bar that already has text, the search button will already be enabled, so a special-case is only needed when the current search text is empty.

func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if text.characters.count > 0 && range.length == 0 && range.location == 0 {
        dispatch_async(dispatch_get_main_queue()) {
            searchBar.resignFirstResponder()
            searchBar.becomeFirstResponder()
        }
    }
    return true
}

Upvotes: 4

Related Questions