Ios 如何使UISearchController()在默认情况下隐藏?
我已将UISearchController()添加到UIViewController中,如下所示:Ios 如何使UISearchController()在默认情况下隐藏?,ios,swift,uisearchcontroller,Ios,Swift,Uisearchcontroller,我已将UISearchController()添加到UIViewController中,如下所示: class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating { let tableData = ["Here's", "to", "the", "crazy"] var filteredTableData = [String]
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()
}
。。。
TableView函数
我试图使搜索栏在默认情况下隐藏,换句话说,使其类似于“Notes”iPhone应用程序中的搜索栏样式,您必须向下滚动tableView以显示搜索栏。有什么办法可以做到这一点吗?就像添加一个负约束,当ViewController加载时,它会隐藏在导航栏下?找到了一个非常简单的解决方案,默认情况下隐藏搜索栏,我找到了答案 我只需要在viewDidLoad()中添加这一行: 更新为Swift 3.0语法:
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
您可以添加,仅当第一节中至少有一行时才应发出此命令。
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)