Я пытаюсь добавить UISearchController
при нажатии кнопки на UINavigationBar
. Затем, когда кнопка снова нажата, она отклоняет строку поиска. До сих пор у меня есть:Удаление UISearchController из 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
., Который был там, также не возвращается, когда я сдвигаю searchBar
с экрана. «м отсутствует что-то для того, чтобы уволить searchController
, но я не знаю, что правильный вызов. searchController
.active свойства не кажется, чтобы сделать это.