【发布时间】:2021-04-22 17:39:35
【问题描述】:
我正在尝试在 tvOS SwiftUI 应用中实现搜索功能。我使用UISearchController 作为最直接的解决方案来做到这一点。我已经将它包裹在符合UIViewControllerRepresentable 的SearchView 中。问题是,看起来焦点引擎拒绝关注视图控制器 UI 的一部分 - UISearchBar 被包装。我可以在模拟器中从我的 Mac 输入搜索查询,以验证搜索是否有效,但当然,它不是真实的。
我尝试将.focusable() 修饰符添加到SearchView,但没有帮助。
还尝试在 UISearchController 和 UISearchContainerViewController 的自定义子类中实现 shouldUpdateFocus、preferredFocusEnvironments 和 didUpdateFocus 回调,但根本没有调用这些回调。
我想我在这里遗漏了一些非常简单的东西。
这是SearchView的代码:
struct SearchView: UIViewControllerRepresentable {
@Binding var text: String
typealias UIViewControllerType = UISearchContainerViewController
typealias Context = UIViewControllerRepresentableContext<SearchView>
func makeUIViewController(context: Context) -> UIViewControllerType {
let controller = UISearchController(searchResultsController: context.coordinator)
controller.searchResultsUpdater = context.coordinator
return UISearchContainerViewController(searchController: controller)
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
func makeCoordinator() -> SearchView.Coordinator {
return Coordinator(text: $text)
}
class Coordinator: UIViewController, UISearchResultsUpdating {
@Binding var text: String
init(text: Binding<String>) {
_text = text
super.init(nibName: nil, bundle: nil)
}
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
text = searchText
}
}
}
还有主要的ContentView(我已经去掉了一些不重要的代码):
struct ContentView: View {
@State var model = ["aa", "ab", "bb", "bc", "cc", "dd", "ee"]
@State var searchQuery: String = ""
var body: some View {
SearchView(text: $searchQuery)
List {
ForEach(model.filter({ $0.hasPrefix(searchQuery) })) { item in
Text(item)
}
}
}
}
【问题讨论】:
标签: swiftui tvos uisearchcontroller