-
Discover search suggestions for Apple TV
Searching your tvOS app just got even better. Get ready to explore the new simplified search interface and learn how to integrate it into your app with UISearchController. Support your global audience with the addition of new international keyboards and languages. Discover how to add search suggestions to your interface and update results with suggestions on the fly. And we'll share some of our favorite tips for adding a great search experience to Apple TV.
Recursos
-
Buscar neste vídeo...
-
-
1:40 - SearchViewController init
private let appData: AppData init(appData: AppData) { self.appData = appData super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } -
1:51 - Search Tab Bar Item
// use the system standard search tab bar item tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarItem.SystemItem.search, tag: 0) -
2:05 - SearchController and SearchContainerViewController Definition
private let searchController: UISearchController private let searchContainerViewController: UISearchContainerViewController -
2:11 - SearchController and SearchContainerViewController Initialization
self.searchController = UISearchController(searchResultsController: self.searchResultsController) self.searchContainerViewController = UISearchContainerViewController(searchController: searchController) -
2:16 - viewDidLoad - Add Child View Controller
override func viewDidLoad() { addChild(searchContainerViewController) searchContainerViewController.view.frame = view.bounds view.addSubview(searchContainerViewController.view) searchContainerViewController.didMove(toParent: self) } -
3:17 - Set searchControllerObservedScrollView
// scroll search controller allong with results collection view searchController.searchControllerObservedScrollView = searchResultsController.collectionView -
3:43 - Assign searchResultsUpdater
searchController.searchResultsUpdater = self -
4:00 - Implement updateSearchResults
func updateSearchResults(for searchController: UISearchController) { if let searchText = searchController.searchBar.text { // get search results for 'searchText' from data source let (results, _) = appData.searchResults(seachTerm: searchText, includePhotos: true, includeVideos: true) searchResultsController.items = results } else { // no search text, show unfiltered results searchResultsController.items = appData.allEntries } } -
4:16 - Create Instance of SearchViewController
let searchViewController = SearchViewController(appData: appData) -
5:30 - UISearchSuggestionItem Example
let suggestion1 = UISearchSuggestionItem(localizedSuggestion: "Result1", localizedDescription: "Result1", iconImage: nil) let suggestion2 = UISearchSuggestionItem(localizedSuggestion: "Result2", localizedDescription: "Result2", iconImage: nil) searchController.searchSuggestions = [suggestion1, suggestion 2] -
7:05 - Implement UISearchSuggestion Properties
var localizedSuggestion: String? { return self.name } var iconImage: UIImage? { return self.isVideo ? UIImage(systemName: "video") : UIImage(systemName: "photo") } -
7:20 - Implement Accessibility Description
var localizedDescription: String? { if (self.isVideo) { return String.localizedStringWithFormat(NSLocalizedString("%@ - Video", comment: ""), self.name) } return String.localizedStringWithFormat(NSLocalizedString("%@ - Photo", comment: ""), self.name) } -
9:01 - Add new UISearchResultsUpdating
func updateSearchResults(for searchController: UISearchController, selecting searchSuggestion: UISearchSuggestion) { if let searchText = searchController.searchBar.text { var includePhotos = true; var includeVideos = true; } } -
9:13 - Inspect Suggestion
// check if the suggestion is for a photo or video if let suggestedEntry = searchSuggestion as? SuggestedEntry { includeVideos = suggestedEntry.isVideo includePhotos = !includeVideos } -
9:21 - Filter Results
// filter the results by to include photos, videos, or both let (results, _) = appData.searchResults(seachTerm: searchText, includePhotos: includePhotos, includeVideos: includeVideos) searchResultsController.items = results
-