The Bottom Line Question:
How do I override the default animation for the dismissal of a searchBar belonging to a UISearchController?
Standard Search Controller Behavior:
Okay so I am trying to create a custom animation for when the UISearchBar that is attached to a UISearchController becomes active. It seems the standard animation expects for the searchBar to begin with a width that takes up the screen. When the animation begins it shrinks the searchBar and fades in a cancel button to the right of it.
What I want to Achieve:
I want to have my searchBar begin in a small state, roughly half the screen width, to allow for two buttons to be placed in the navbar next to it as well.
Present Animation:
When the searchBar becomes active I want the animation to expand the searchBar and for the cancel button to fade in.
Dismissing Animation:
When the searchBar is dismissed I want the exact opposite animation to occur: Cancel button fade out and searchBar to shrink to it's original size.
The Problem:
I have found a way to achieve the desired presenting animation by using a UISearchControllerDelegate method, presentSearchController:
func presentSearchController(searchController: UISearchController) {
// Animate Buttons
UIView.animateWithDuration(0.1, animations: {
// First Hide Buttons
self.createMoxyButton.alpha = 0
self.centerMapButton.alpha = 0
})
// Animate Search Bar
UIView.animateWithDuration(0.5, animations: {
// Search Bar
searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, self.wBounds - 40, searchController.searchBar.frame.height)
self
})
}
but I have not been able to achieve the dismissal animation. I have tried using the didDismissSearchController: and willDismissSearchController: delegate methods but it results in weird behavior and does not use the animation of frames that I set in these respective delegate methods. When the searchBar is dismissed it will expand to the full screen width, while fading out cancel button, then it will immediately change the frame of the searchBar back to the original size, ignoring my animation. I have also tried using the removeAllAnimation() method to try and stop the default animation from taking place, but to no avail.
func didDismissSearchController(searchController: UISearchController) {
searchController.searchBar.layer.removeAllAnimations()
// Animate
UIView.animateWithDuration(0.5, animations: {
// Show hidden buttons
self.createMoxyButton.alpha = 1
self.centerMapButton.alpha = 1
// Search Bar
searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, self.wBounds - 10 - self.createMoxyButton.frame.size.width - 20 - self.centerMapButton.frame.size.width - 20, searchController.searchBar.frame.height)
self
})
}
Image of Problem Dismissing SearchBar
Gif Animation begins with the searchBar in the Active state with the cancel button visible