我目前有一个应用程序,其导航栏顶部有一个搜索图标/按钮。
我不确定 Storyboard是否可行,但我想显示一个带有自定义表格 View 和关闭按钮的搜索 Controller ,很像维基百科应用程序
看下面的例子
http://i.imgur.com/ggsTj6W.png
http://i.imgur.com/BVDLU2O.jpg
所以工作流程是: - 按搜索图标, - 表格 View 带有以关闭按钮为中心的搜索 - 搜索结果作为来自可重用源的自定义单元格出现在表格 View 中
当我在维基百科应用程序上使用此搜索时,它感觉非常适合 ios,所以我认为有办法做到这一点?
谢谢
最佳答案
创建一个
UITableViewController
对象并使其符合协议(protocol)UISearchResultsUpdating
将搜索 Controller 添加到类中
let searchController = UISearchController(searchResultsController: nil)
在viewdidload方法中添加如下代码
self.definesPresentationContext = true searchController.searchResultsUpdater = self searchController.hidesNavigationBarDuringPresentation = true searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.sizeToFit() self.tableView.tableHeaderView = searchController.searchBar
实现以下方法以符合
UISearchResultsUpdating
协议(protocol)func updateSearchResultsForSearchController(searchController: UISearchController)
在主页面上(您想要显示搜索按钮的地方),为上面的 tableViewController 创建一个 segue,并将其设置为在点击按钮时以模态方式呈现。
关于带有自定义单元格的 Ios/swift/UISearchController 或 SearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515357/