在没有故事板的情况下使用 UISearchController
Use UISearchController without storyboard
我对 iOS 比较陌生(100 小时左右),我正在尝试实现没有故事板的 UISearchController。是否可以通过编程方式执行此操作?
您必须以编程方式实现 UISearchController
,因为 Apple 尚未提供使用 Storyboard 进行设置的功能。
以下是在视图控制器
中实现UISearchController
的步骤
- 符合
UISearchResultsUpdating
协议
创建一个变量来引用 UISearchController
var searchController: UISearchController!
在viewDidLoad(_:)
中设置searchController
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 比较陌生(100 小时左右),我正在尝试实现没有故事板的 UISearchController。是否可以通过编程方式执行此操作?
您必须以编程方式实现 UISearchController
,因为 Apple 尚未提供使用 Storyboard 进行设置的功能。
以下是在视图控制器
中实现UISearchController
的步骤
- 符合
UISearchResultsUpdating
协议 创建一个变量来引用
UISearchController
var searchController: UISearchController!
在
中设置viewDidLoad(_:)
searchController
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 }