如何将 UISearchController 放置到 navigationTitle 以及如何通过按钮启用和禁用它?
How to place UISearchController to the navigationTitle and how to enable and disable it by button?
我是 Swift 的新手,想知道是否有可能将 UIsearchcontroller 放在 navgationtitle 中,并通过位于 navigationRightItem 的按钮隐藏和取消隐藏 uisearchcontroller。
我见过很多将 Object 库中的 UISearchBar 放置到导航标题的示例,但是没有关于 uisearchcontroller 的示例。甚至有人说 object 库中的 UISearch 栏已从 ios10 Swift 中弃用。
请澄清弃用以及为什么我应该避免使用 object 库中的 UISearchBar。
哪个是用于 firebase tableview 搜索的最佳实践。
如果您能提供代码示例,我将很高兴。
提前谢谢你。
UISearchBar
UISearchBar 尚未弃用。 UISearchBar 是一个 UIView,您可以将其放置在您想要的任何位置,包括导航栏,但如果您愿意,也可以将其放置在屏幕中间。您可以通过编程方式执行此操作,也可以使用 Interface Builder 拖动搜索栏并将其放置在您的一个视图中。
使用 UISearchBar
实现搜索的一种方法是使用 UISearchBar,将其放入您 navigationItem.titleView,并将您的视图控制器设置为该搜索栏的委托。当用户与搜索栏交互时,您的视图控制器会收到通知,您必须在同一个视图控制器中处理搜索逻辑。例如,您可能需要检测搜索是否处于活动状态,以决定要为您的 table 视图使用什么数据源 - 如果搜索不活动,则显示所有项目;如果搜索处于活动状态,则只显示项目的一个子集。或者,您可能想为主视图显示一种布局,为搜索结果显示另一种布局。同样,您必须在视图控制器中处理此逻辑。这是 UISearchController 派上用场的地方!
使用 UISearchController
UISearchController 是为您提供 UISearchBar 的 UIViewController。 UISearchController 很有用,因为它可以帮助您在新的视图控制器中显示搜索结果,使您的搜索逻辑与主要逻辑分开。
要解决您问题中的特定问题,请记住您放入 navigationItem.titleView 的内容是 UISearchBar,而不是 UISearchController。您可以自己创建此 UISearchBar,也可以创建 UISearchController 并改用其搜索栏 (searchController.searchBar)。
将 UISearchBar 放置在 navigationItem.titleView VS 使用 navigationItem.searchController 属性
将您的 UISearchBar 放在 navigationItem.titleView 中是多个应用程序中使用的常见模式。但是,在 iOS 11 中,Apple 推出了 navigationItem.searchController 属性。这里没有任何反对意见,但是当涉及到在导航栏中使用搜索栏时,Apple 似乎更希望我们这样做。
下面是这可能如何工作的示例。
您的主视图控制器:
class HomeViewController: UIViewController {
(…)
override func viewDidLoad() {
(…)
// Get the view controller that will be used to present search results.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let languagesSearchResultsViewController = storyboard.instantiateViewController(withIdentifier: "LanguagesSearchResultsViewController") as? LanguagesSearchResultsViewController else { return }
// Create a search controller and initialize it with the view controller that will be used to show the search results
let searchController = UISearchController(searchResultsController: languagesSearchResultsViewController)
// The searchResultsUpdater is the object that will be notified when the user performs a search.
// I'm setting it to be the view controller that will present the results but is doesn't have to be.
searchController.searchResultsUpdater = languagesSearchResultsViewController
// Set the searchController property in the navigationItem and a search bar will automagically appear in the navigation bar, below the title.
navigationItem.searchController = searchController
definesPresentationContext = true
(…)
}
}
您将用来显示搜索结果的视图控制器:
class LanguagesSearchResultsViewController: UIViewController {
// MARK: - Properties
// Complete list of items to search. If you're doing a network request for your search term then you don't need this.
let languages = ["Mandarin Chinese", "English", "Hindustani", "Spanish", "Arabic", "Malay", "Russian", "Bengali", "Portuguese", "French", "Hausa", "Punjabi", "German", "Japanese", "Persian", "Swahili", "Telugu", "Javanese", "Wu Chinese", "Korean"]
// The data source for your search results
var searchResults: [String] = []
// MARK: - IBOutlets
@IBOutlet weak var tableView: UITableView!
// MARK: - UIViewController methods
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
(…)
}
// MARK: - UISearchResultsUpdating
extension LanguagesSearchResultsViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
searchResults = languages.filter { [=11=].contains(searchText) }
tableView.reloadData()
}
}
// MARK: - UITableViewDataSource
extension LanguagesSearchResultsViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "LanguageTableViewCell") as? LanguageTableViewCell else { return UITableViewCell() }
let language = searchResults[indexPath.row]
cell.languageNameLabel.text = language
return cell
}
}
如果您仍然希望 titleView 中的搜索栏具有您所描述的 show/hide 行为,则您需要更自定义的东西,使用或不使用 UISearchController。已经有许多其他问题的答案可以帮助您做到这一点。希望这将有助于阐明您的选择。
我是 Swift 的新手,想知道是否有可能将 UIsearchcontroller 放在 navgationtitle 中,并通过位于 navigationRightItem 的按钮隐藏和取消隐藏 uisearchcontroller。 我见过很多将 Object 库中的 UISearchBar 放置到导航标题的示例,但是没有关于 uisearchcontroller 的示例。甚至有人说 object 库中的 UISearch 栏已从 ios10 Swift 中弃用。 请澄清弃用以及为什么我应该避免使用 object 库中的 UISearchBar。 哪个是用于 firebase tableview 搜索的最佳实践。 如果您能提供代码示例,我将很高兴。 提前谢谢你。
UISearchBar
UISearchBar 尚未弃用。 UISearchBar 是一个 UIView,您可以将其放置在您想要的任何位置,包括导航栏,但如果您愿意,也可以将其放置在屏幕中间。您可以通过编程方式执行此操作,也可以使用 Interface Builder 拖动搜索栏并将其放置在您的一个视图中。
使用 UISearchBar
实现搜索的一种方法是使用 UISearchBar,将其放入您 navigationItem.titleView,并将您的视图控制器设置为该搜索栏的委托。当用户与搜索栏交互时,您的视图控制器会收到通知,您必须在同一个视图控制器中处理搜索逻辑。例如,您可能需要检测搜索是否处于活动状态,以决定要为您的 table 视图使用什么数据源 - 如果搜索不活动,则显示所有项目;如果搜索处于活动状态,则只显示项目的一个子集。或者,您可能想为主视图显示一种布局,为搜索结果显示另一种布局。同样,您必须在视图控制器中处理此逻辑。这是 UISearchController 派上用场的地方!
使用 UISearchController
UISearchController 是为您提供 UISearchBar 的 UIViewController。 UISearchController 很有用,因为它可以帮助您在新的视图控制器中显示搜索结果,使您的搜索逻辑与主要逻辑分开。 要解决您问题中的特定问题,请记住您放入 navigationItem.titleView 的内容是 UISearchBar,而不是 UISearchController。您可以自己创建此 UISearchBar,也可以创建 UISearchController 并改用其搜索栏 (searchController.searchBar)。
将 UISearchBar 放置在 navigationItem.titleView VS 使用 navigationItem.searchController 属性
将您的 UISearchBar 放在 navigationItem.titleView 中是多个应用程序中使用的常见模式。但是,在 iOS 11 中,Apple 推出了 navigationItem.searchController 属性。这里没有任何反对意见,但是当涉及到在导航栏中使用搜索栏时,Apple 似乎更希望我们这样做。
下面是这可能如何工作的示例。
您的主视图控制器:
class HomeViewController: UIViewController {
(…)
override func viewDidLoad() {
(…)
// Get the view controller that will be used to present search results.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let languagesSearchResultsViewController = storyboard.instantiateViewController(withIdentifier: "LanguagesSearchResultsViewController") as? LanguagesSearchResultsViewController else { return }
// Create a search controller and initialize it with the view controller that will be used to show the search results
let searchController = UISearchController(searchResultsController: languagesSearchResultsViewController)
// The searchResultsUpdater is the object that will be notified when the user performs a search.
// I'm setting it to be the view controller that will present the results but is doesn't have to be.
searchController.searchResultsUpdater = languagesSearchResultsViewController
// Set the searchController property in the navigationItem and a search bar will automagically appear in the navigation bar, below the title.
navigationItem.searchController = searchController
definesPresentationContext = true
(…)
}
}
您将用来显示搜索结果的视图控制器:
class LanguagesSearchResultsViewController: UIViewController {
// MARK: - Properties
// Complete list of items to search. If you're doing a network request for your search term then you don't need this.
let languages = ["Mandarin Chinese", "English", "Hindustani", "Spanish", "Arabic", "Malay", "Russian", "Bengali", "Portuguese", "French", "Hausa", "Punjabi", "German", "Japanese", "Persian", "Swahili", "Telugu", "Javanese", "Wu Chinese", "Korean"]
// The data source for your search results
var searchResults: [String] = []
// MARK: - IBOutlets
@IBOutlet weak var tableView: UITableView!
// MARK: - UIViewController methods
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
(…)
}
// MARK: - UISearchResultsUpdating
extension LanguagesSearchResultsViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
searchResults = languages.filter { [=11=].contains(searchText) }
tableView.reloadData()
}
}
// MARK: - UITableViewDataSource
extension LanguagesSearchResultsViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "LanguageTableViewCell") as? LanguageTableViewCell else { return UITableViewCell() }
let language = searchResults[indexPath.row]
cell.languageNameLabel.text = language
return cell
}
}
如果您仍然希望 titleView 中的搜索栏具有您所描述的 show/hide 行为,则您需要更自定义的东西,使用或不使用 UISearchController。已经有许多其他问题的答案可以帮助您做到这一点。希望这将有助于阐明您的选择。