如何在搜索页面上将searchBar设置为Siri的全局焦点?

时间:2017-01-31 15:15:31

标签: swift tvos siri

描述

在我的tvOS应用程序中,我的搜索页面位于Search Bar顶部,Table View下有最终结果列表。

当我们关注searchBar并且用户按下Siri的语音时,结果会插入搜索栏(如预期的那样)。但是,当用户向下滚动搜索结果并且无法找到他们要查找的内容时,他们必须向上滚动(一直向上)到searchBar再次使用Siri。

如果用户在searchBar不在焦点时尝试使用Siri,全球Siri会开始在不同的应用中寻找结果(这不是我想要的)

问题:

如何在搜索页面上将searchBar设置为Siri的全局焦点?

我尝试了什么:

说实话,我不知道该怎么做......

在AppDelegate.swift中调用一个函数来创建Search View Controller并将其添加到Tab Bar Controller。

我考虑过尝试preferedFocusView,但在阅读完文档之后,我怀疑它会起作用。

func configueSearchController() -> UIViewController {

        let storyboard = UIStoryboard(name: "Search", bundle: nil)
        guard let searchResultsController = storyboard.instantiateViewController(withIdentifier: SearchViewController.storyboardIdentifier) as? SearchViewController else {
            fatalError("Unable to instatiate a SearchResultViewController from the storyboard.")
        }

        /*
         Create a UISearchController, passing the `searchResultsController` to
         use to display search results.
         */

        let searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.searchBar.placeholder = NSLocalizedString("Enter keyword (e.g. Gastric Bypass)", comment: "")
        searchController.view.backgroundColor = Constants.Color.backgroundcolor
        searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.dark
        searchController.searchBar.tintColor = Constants.Color.backgroundcolor
        searchController.searchBar.backgroundColor = Constants.Color.backgroundSearchBarColor
        searchController.searchBar.setTextColor(color: .white)
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = true
        searchController.searchBar.searchBarStyle = .minimal
        searchController.searchBar.sizeToFit()
        searchController.searchBar.focus


        //TODO: Check if this works
        searchController.searchBar.accessibilityLanguage = "en"

        //searchResultsController.tableView.tableHeaderView = searchController.searchBar

        // Contain the `UISearchController` in a `UISearchContainerViewController`.
        let searchContainer: UISearchContainerViewController = UISearchContainerViewController(searchController: searchController)

        // Finally contain the `UISearchContainerViewController` in a `UINavigationController`.
        let searchNavigationController = UINavigationController(rootViewController: searchContainer)
        searchNavigationController.navigationBar.isTranslucent = true
        searchNavigationController.navigationBar.tintColor = Constants.Color.backgroundcolor
        searchNavigationController.tabBarItem.title = "Search"
        return searchNavigationController

}

1 个答案:

答案 0 :(得分:0)

请阅读http://nerds.airbnb.com/tvos-focus-engine/以使控件具有针对性。