我在向UINavigationBar
按下按钮时尝试添加@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
这会添加navitionItem
并将其从屏幕的RHS中滑入。再次按下该按钮时,它会将搜索栏向后滑动到右侧。但是,搜索栏覆盖的后退按钮无法重置以说出单词"返回,原来的searchBar
。标题在我滑动时也没有回来searchController
关闭屏幕。我觉得我错过了一些东西以便解雇searchController
,但我不知道正确的电话是什么。 <Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ConstantsStyle.xaml"/>
<ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
......
</Application.Resources>
。有效财产似乎没有这样做。
答案 0 :(得分:0)
调用dismissViewControllerAnimated:
以关闭UISearchController
并执行方法完成块中的所有其他更改。