Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss keyboard in UISearchController when changing focus in tvos?

I'm completely new to tvos and I'm trying to implement a UISearchController view where, in my SearchResultsViewController, I have two UICollectionViews displayed one above the other: enter image description here

The problem is that when the user swipes down to select one of the items in the UICollectionView, the keyboard doesn't dismiss. Even swiping back up to select the keyboard doesn't fully scroll up and it's impossible to see what you're typing. The resulting view is this: enter image description here

Ideally, I'd like to dismiss the keyboard when the user swipes down to focus on anything else in the interface. I looked at Apple's tvos UIKit Catalog and their example shows a UISearchController which dismisses the keyboard when changing focus, but I don't see that they're doing anything differently.

Here is the code I'm using to setup my UISearchController when the user clicks on a button:

@IBAction func onSearchButton(sender: AnyObject) {
    guard let resultsController = storyboard?.instantiateViewControllerWithIdentifier(SearchResultsViewController.storyboardID) as? SearchResultsViewController else { fatalError("Unable to instantiate a SearchResultsViewController.") }

    // Create and configure a `UISearchController`.
    let searchController = UISearchController(searchResultsController: resultsController)
    searchController.searchResultsUpdater = resultsController
    searchController.hidesNavigationBarDuringPresentation = false

    let searchPlaceholderText = NSLocalizedString("Search for a Show or Movie", comment: "")
    searchController.searchBar.placeholder = searchPlaceholderText

    // Present the search controller from the root view controller.
    guard let rootViewController = view.window?.rootViewController else { fatalError("Unable to get root view controller.") }
    rootViewController.presentViewController(searchController, animated: true, completion: nil)
}
like image 231
Mel Avatar asked Dec 21 '15 13:12

Mel


2 Answers

After quite a bit of trial and error, I was able to figure out the solution.

The keyboard will automatically dismiss itself as long as: 1) The item the user focuses on is inside of a scrollview 2) The scrollview content size is larger than the screen height by at least 1px (1081px).

like image 95
Mel Avatar answered Nov 08 '22 23:11

Mel


After quite a lot of trial and error, finally I figured out. The reason is that you have nested ScrollViews in searchResultsController.

"ScrollViews" of-course includes UICollectionView, UITableView, and UIScrollView.

According to my investigation, UISearchController behaves as follows.

  1. If the first view which gets focused in searchResultsController is subview of the inner scrollView (which is the horizontal UICollectionView, in your case), then you won't get keyboard hidden as expected.

  2. Interestingly, if the first view which gets focused in searchResultsController is subview of outer scrollView, then you will get keyboard hidden completely, animated, just as expected (!).

I think this is sort of UIKit's bug.

like image 3
toshi0383 Avatar answered Nov 08 '22 23:11

toshi0383