我对iOS相对较新(大约100个小时左右),并且正在尝试实现没有故事板的UISearchController。是否可以通过编程方式执行此操作?
最佳答案
由于Apple尚未提供使用Storyboard进行设置的功能,因此您必须以编程方式实现UISearchController
。
以下是在视图控制器中实现UISearchController
的步骤
UISearchResultsUpdating
协议UISearchController
var searchController: UISearchController!
searchController
中设置viewDidLoad(_:)
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
// The object responsible for updating the contents of the search results controller.
searchController.searchResultsUpdater = self
// Determines whether the underlying content is dimmed during a search.
// if we are presenting the display results in the same view, this should be false
searchController.dimsBackgroundDuringPresentation = false
// Make sure the that the search bar is visible within the navigation bar.
searchController.searchBar.sizeToFit()
// Include the search controller's search bar within the table's header view.
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
}
updateSearchResultsForSearchController(_:)
,当搜索栏成为第一响应者或用户对搜索栏内的文本进行更改时调用该方法func updateSearchResultsForSearchController(searchController: UISearchController) {
// No need to update anything if we're being dismissed.
if !searchController.active {
return
}
// you can access the text in the search bar as below
filterString = searchController.searchBar.text
// write some code to filter the data provided to your tableview
}
关于ios - 在没有 Storyboard 的情况下使用UISearchController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29285066/