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

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

The problem is that when the user swipes down to select one of the elements in the UICollectionView, the keyboard does not close. Even if you slide your finger up to select a keyboard, you won’t be able to fully scroll up and you won’t be able to see what you are typing. The resulting view looks like this: enter image description here

Ideally, I would like to remove the keyboard when the user swipes down to focus on something else in the interface. I looked at the Apple tvos UIKit directory, and their example shows a UISearchController that rejects the keyboard when the focus changes, but I don’t see that they do something different.

Here is the code that I use to configure my UISearchController when a user clicks 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) } 
+6
source share
4 answers

After a few trial and error, I was able to find a solution.

The keyboard will automatically turn off if: 1) The element that the user focuses on is inside the scroll 2) The size of the scroll content is greater than the screen height of at least 1px (1081px).

+4
source

After quite a lot of trial and error, I finally understood. The reason is because you have nested ScrollViews in the searchResultsController.

The “ScrollViews” from the course include UICollectionView, UITableView, and UIScrollView.

According to my research, the UISearchController behaves as follows.

  • If the first view that is focused in the searchResultsController is subordinate to the internal scrollView (which is the horizontal UICollectionView, in your case), then you won't get the hidden keyboard as expected.

  • Interestingly, if the first view, which is focused in searchResultsController, is a subordinate external scrollView, then you will get a completely hidden keyboard, animated as expected (!).

I think this is kind of a UIKit error.

+3
source

I had exactly the same layout and so far have not been able to achieve this. I believe that you return false in tableView (tableView: UITableView, canFocusRowAtIndexPath indexPath: NSIndexPath) -> Bool so that each cell in the collection view can scroll horizontally with the correct focal behavior. I think this is actually causing problems. If you make the first cell in table view, the problem will disappear, but, of course, focal behavior is undesirable. I just found this today and will try more tomorrow to find out what I can do about it. I feel that I will need a new design that allows me to use a single collection view or tableview that has its cells oriented in the resultsController. Hope this is easily achievable in tvOS 10.

0
source

Work around the solution: add one dummy cell to indexPath.row == 0 with a height of 1 pixel and enable focus on it.

0
source

Source: https://habr.com/ru/post/1238840/


All Articles