Does somebody have all ready implemented searchbar on tvos with swiftui?

前端 未结 1 342
孤独总比滥情好
孤独总比滥情好 2021-01-23 14:46

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

I tried this UISearchBar(frame: .zero) b

相关标签:
1条回答
  • 2021-01-23 15:45

    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()
        }
    }
    
    0 讨论(0)
提交回复
热议问题