gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:18:24 24 4
gpt4 key购买 nike

描述

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

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

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

问题:

如何在Search Page上设置searchBar为Siri的全局焦点?

我尝试过的:

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

在 AppDelegate.swift 中调用一个函数来创建搜索 View Controller 并将其添加到标签栏 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

}

最佳答案

请阅读http://nerds.airbnb.com/tvos-focus-engine/使控件可聚焦。

关于swift - 如何在搜索页面上将searchBar设置为Siri的全局焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41961060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com