Search code examples
swift2tvosuisearchcontrollerapple-tv

tvOS: A way to have UISearchController not appear as a button?


This code

var searchController: UISearchController!
@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    view.addSubview(searchController.searchBar)
    getItems()
}

produces this: Screeshot1

Note that the searchBar appears as a button stuck in the upper left (because this is a tabbed app it appears under the tab bar when first presented. The "Button" button is there to receive focus for testing).

This is how it looks after pressing the button: Screenshot2

The second image shows how I would like things to look when the search tab opens, and the way I though it was supposed to look in tvOS.

How do I get the searchController to appear as it does in the second screenshot? Many tvOS apps do this, so I know it must be possible. I have been over the documentation carefully, but I must have missed something.

A related is issue is that the collectionView below won't take focus from the searchController. One has to go back using the remote menu button to get the collectionView to focus.

How can I get the searchController to appear as it does in the second screenshot when the view appears?

How can I get the collectionView to take focus from the searchController without having to go back to the tab bar?


Solution

  • is I finally ran across this passage in the tvOS developer library

    All of the iOS first responder mechanisms work on tvOS. This allows developers to present a UI, visible or hidden, and then make one of the text fields the first responder by calling the becomeFirstResponder method on it. This action causes the text field to put up the keyboard UI without the user having to navigate to a text field and click on it.

    so, adding

    searchController.searchBar.becomeFirstResponder()
    

    displays the inline keyboard that I wanted users to start with. However, one has to press the menu button on the remote before the focus engine kicks in again. The menu button also dismisses the keyboard and searchBar returns to its button state.

    This an answer to the first of my questions. Still clueless about the second.