如何使 UISearchController() 默认隐藏?
how to make the UISearchController() hidden by default?
我已将 UISearchController() 添加到我的 UIViewController,如下所示:
class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
let tableData = ["Here's", "to", "the", "crazy"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
}
...
表格视图函数
...
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
我正在尝试使搜索栏默认隐藏,换句话说,使其类似于 "Notes" iPhone 应用程序的搜索栏样式,您必须向下滚动 tableView显示搜索栏。有什么办法可以做到这一点?就像添加一个负约束,使其在 ViewController 加载时隐藏在导航栏下方?
找到了一个非常简单的默认隐藏搜索栏的解决方案,我找到了答案here
我只需要在 viewDidLoad() 中添加这一行:
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
已更新为 Swift 3.0 语法:
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
我已将 UISearchController() 添加到我的 UIViewController,如下所示:
class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
let tableData = ["Here's", "to", "the", "crazy"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
}
... 表格视图函数 ...
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
我正在尝试使搜索栏默认隐藏,换句话说,使其类似于 "Notes" iPhone 应用程序的搜索栏样式,您必须向下滚动 tableView显示搜索栏。有什么办法可以做到这一点?就像添加一个负约束,使其在 ViewController 加载时隐藏在导航栏下方?
找到了一个非常简单的默认隐藏搜索栏的解决方案,我找到了答案here
我只需要在 viewDidLoad() 中添加这一行:
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
已更新为 Swift 3.0 语法:
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)