当按下 UINavigationBar
按钮时,我试图添加一个 UISearchController
。然后当再次按下按钮时,它会关闭搜索栏。到目前为止,我有:
@IBAction func showSearchBar(sender: UIBarButtonItem) {
if searchController == nil {
var resultsController = SearchResultsViewController()
resultsController.searchResultsDelegate = self
searchController = UISearchController(searchResultsController: resultsController)
searchController!.searchResultsUpdater = resultsController
searchController!.searchBar.frame = CGRect(
x: searchController!.searchBar.frame.origin.x,
y: searchController!.searchBar.frame.origin.y,
width: searchController!.searchBar.frame.size.width,
height: 44.0)
searchController!.searchBar.center.x += self.view.bounds.width
}
if !searchBarActive {
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.searchController!.searchBar.center.x -= self.view.bounds.width
}, completion: nil)
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.searchBarStyle = UISearchBarStyle.Default
searchController?.dimsBackgroundDuringPresentation = true
navigationItem.titleView = searchController?.searchBar
self.definesPresentationContext = true
} else {
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.searchController!.searchBar.center.x += self.view.bounds.width
}, completion: nil)
dismissViewControllerAnimated(true, completion: nil) // edited: tried this per comments but also does not work
searchController!.active = false
}
searchBarActive = !searchBarActive
}
这确实添加了 searchController.searchBar
并将其从屏幕的 RHS 滑入。再次按下该按钮时,它会将搜索栏滑回右侧。但是,我的被搜索栏覆盖的后退按钮不会重置为“返回”字样,当我滑动 searchBar
关闭屏幕。我觉得我为了关闭 searchController
而遗漏了一些东西,但我不知道正确的调用是什么。searchController
.active 属性似乎没有这样做。
最佳答案
调用 dismissViewControllerAnimated:
关闭 UISearchController
并在该方法的完成 block 中执行所有其他更改。
关于ios - 不活动时从 UINavigationBar 中删除 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159794/