如何覆盖属于UISearchController的searchBar的默认动画?
好的,所以我正在尝试为附加到UISearchController的UISearchBar变为活动状态时创建自定义动画.似乎标准动画期望searchBar以占据屏幕的宽度开始.动画开始时缩小搜索栏并在其右侧的取消按钮中淡入淡出.
我希望我的searchBar以小的状态开始,大约是屏幕宽度的一半,以便允许两个按钮放在它旁边的导航栏中.
当searchBar变为活动状态时,我希望动画扩展searchBar并使取消按钮淡入.
当searchBar被解除时,我希望发生完全相反的动画:取消按钮淡出和searchBar缩小到它的原始大小.
我找到了一种方法,通过使用UISearchControllerDelegate方法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
})
}
Run Code Online (Sandbox Code Playgroud)
但我无法实现解雇动画.我已经尝试使用didDismissSearchController:和willDismissSearchController:委托方法但它导致奇怪的行为,并且不使用我在这些相应的委托方法中设置的帧的动画.当searchBar被解除时,它将扩展到全屏宽度,同时淡出取消按钮,然后它会立即将searchBar的帧更改回原始大小,忽略我的动画.我也尝试使用removeAllAnimation()方法尝试阻止默认动画发生,但无济于事.
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 = …Run Code Online (Sandbox Code Playgroud)