我通过 UISearchController 在我的测试应用程序中实现了搜索栏。当我启动应用程序时,我会在导航控制器下方看到搜索栏。但如何在应用程序启动时隐藏它并仅在下拉表格视图时显示它?并在拉出表格视图时再次隐藏?我在google或youtube上找不到任何解决方案,请帮忙。
EDIT:这就是我实现 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)
将以下部分放入您的viewDidLoad
,所以发生的事情是你问UITableView
滚动并隐藏SearchBar
一开始:
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
并且不要忘记设置TableViewHeader
给你的searchController
:
tableView.tableHeaderView = searchController.searchBar
Update:
因为您有一个空的表视图,所以替换以下内容:
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
With :
self.tableView.contentOffset = CGPointMake(0.0, 44.0)
祝你好运 !
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)