我正在开发一个TVOS应用程序。我使用下面的代码在UITabBarController中实现搜索功能。显示标签栏时的问题,然后标签栏覆盖搜索栏。见下面的截图。如果显示标签栏,是否可以向下移动searchBar位置?
let searchViewController = VVSearchViewController()
let searchController = UISearchController(searchResultsController: searchViewController)
searchViewController.searchViewController = searchController
searchController.searchResultsUpdater = searchViewController
searchController.obscuresBackgroundDuringPresentation = true
let searchPlaceholderText = "Search"
searchController.searchBar.placeholder = searchPlaceholderText
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.barTintColor = UIColor.black
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.keyboardAppearance = .dark
searchController.view.backgroundColor = UIColor.black
searchController.definesPresentationContext = true;
UISearchContainerViewController(searchController: searchController)
答案 0 :(得分:0)
您可以在选项卡的搜索中找到完全相同的案例" App Store"。
这是因为在tvOS中,viewController的内容不应该对标签栏的可见性做出反应。标签栏显示在其上方,重叠。
用于标签栏的tvOS人机界面指南: https://developer.apple.com/tvos/human-interface-guidelines/interface-elements/tab-bars/
答案 1 :(得分:0)
有可能。 我花了一段时间,但我的想法是将搜索控制器添加到顶部约束的“容器”控制器中。如果尝试将UISearchController作为子视图控制器添加到容器中,则不会显示任何内容。 UISearchContainerViewController不能正常工作。但是以UISearchContainerViewController为根的导航控制器有效。这是我的代码:
// Create the search controller and it's results controller
let resultsController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SearchResultsViewController") as! SearchResultsViewController
let searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = resultsController
// Customize it
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [.foregroundColor: UIColor.text]
searchController.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController.searchBar.tintColor = .text
searchController.searchBar.barTintColor = .text
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.keyboardAppearance = .dark
// Embed it in UISearchContainerViewController
let searchContainerViewController = UISearchContainerViewController(searchController: searchController)
// Make UISearchContainerViewController root controller of a navigation controller
let navController = UINavigationController(rootViewController: searchContainerViewController)
// And a container to hold everything
let containerViewController = UIViewController()
// Do a standard child add of navController into containerViewController
navController.loadViewIfNeeded()
navController.viewWillAppear(false)
navController.willMove(toParent: containerViewController)
containerViewController.view.addSubview(navController.view)
navController.view.frame = containerViewController.view.bounds
// Add your constraints. In this case I just want the search bar to be 150 px down.
NSLayoutConstraint.activate([
navController.view.topAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.topAnchor, constant: 150),
navController.view.leftAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.leftAnchor),
navController.view.bottomAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.bottomAnchor),
navController.view.rightAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.rightAnchor)
]) containerViewController.addChild(navController)
containerViewController.addChild(navController)
navController.didMove(toParent: containerViewController)
navController.viewDidAppear(false)
// And from here use the containerViewController as normal to display a search into your app.
注意:如果没有在navController上传递viewDidAppear,它将无法正常工作。 注意2:我仅在tvOS 13和14上测试过。