我通过UISearchController在我的测试应用程序中实现了搜索栏.当我启动我的应用程序时,我看到导航控制器下的搜索栏.但是我怎么能在应用程序启动时隐藏它并仅在我拉下桌面视图时显示它?拉起桌面视图时再次隐藏?我在google或youtube上找不到任何解决方案,请帮忙.
编辑:
这是我实现UISearchController的方式viewDidLoad
//set the searchController
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search your restaurant"
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
Run Code Online (Sandbox Code Playgroud)
将以下部分放在您的中viewDidLoad,所以发生的事情是您要求UITableView滚动并隐藏SearchBar在开头:
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
Run Code Online (Sandbox Code Playgroud)
并且不要忘记设置TableViewHeader为searchController:
tableView.tableHeaderView = searchController.searchBar
Run Code Online (Sandbox Code Playgroud)
更新:
因为您有一个空的tableview,所以替换以下内容:
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
用:
self.tableView.contentOffset = CGPointMake(0.0, 44.0)
Run Code Online (Sandbox Code Playgroud)
祝好运 !