简体   繁体   中英

UISearchBar resigns first responder when trying to select UICollectionViewCell

I have a UIViewController that functions as a sharing drawer, and slides up from the bottom of the screen.

I have a UISearchBar and UICollectionView below it, typing in the search bar updates the collection view without a problem.

When the search bar is active, I want to be able to select cells in the collection view. However, whenever I tap anything other than the search bar, the keyboard is dismissed. I am used to having to add a UITapGestureRecognizer or manually calling searchBar.resignFirstResponder() , but in this case I cannot figure out how to NOT dismiss the keyboard.

There are no other gesture recognizers on the view controller. Any ideas on allowing (multiple) selection of the collection view while the keyboard is present?

To unhide keyboard

self.view.endEditing(false)

are you looking for this?


as in case of

resignFirstResponder

you cannot pass a boolean to hide or unhide



We can also do this with following methods by always adding cancel button programmatically to search bar

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchBar.showsCancelButton = true;
    }

Also, should be trying by dropping an IBOutlet of the search bar as:

self.yourSearchBar.endEditing(false)

Note: yourSearchBar is the name of your search bar provided on dropping IBOutlet.

@nayem's comment made me think of the searchBarShouldEndEditing delegate method. The search bar does send this message prior to resigningFirstResponder and I can return false . Problem solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM