本文介绍了有没有人在 tvos 上用 swiftui 准备好了搜索栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人已经准备好在 tvos 上使用 Apple 组件(例如 UISearchBar 和 swiftui)实现搜索栏?

Does somebody have all ready implemented a search bar using Apple component like UISearchBar with swiftui on tvos ?

我试过这个 UISearchBar(frame: .zero) 但我得到这个错误 init(frame:)' is available in tvOS

I tried this UISearchBar(frame: .zero) but I got this error init(frame:)' is unavailable in tvOS

我只找到了 ios 的解决方案 ..

I only found solutions for ios ..

谢谢:D

推荐答案

初始设置方案应如下所示.当然,搜索/过滤/显示结果的逻辑是特定于应用的.

The scheme of initial setup should be like below. Of course, the logic of searching/filtering/showing results is app specific.

import SwiftUI
import TVUIKit

struct SearchView: UIViewControllerRepresentable {

    func makeUIViewController(context: UIViewControllerRepresentableContext<SearchView>) -> UINavigationController {
        let controller = UISearchController(searchResultsController: context.coordinator)
        controller.searchResultsUpdater = context.coordinator
        return UINavigationController(rootViewController: UISearchContainerViewController(searchController: controller))
    }

    func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<SearchView>) {
    }

    func makeCoordinator() -> SearchView.Coordinator {
        Coordinator()
    }

    typealias UIViewControllerType = UINavigationController

    class Coordinator: UIViewController, UISearchResultsUpdating {
        func updateSearchResults(for searchController: UISearchController) {
            // do here what's needed
        }
    }
}

struct ContentView: View {
    @State private var text: String = ""
    var body: some View {
        VStack {
            SearchView()
            Spacer()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这篇关于有没有人在 tvos 上用 swiftui 准备好了搜索栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 22:46