Ezi00 Ezi00 - 2 months ago 15x
Swift Question

UISearchBar showsCancelButton animation complete

I need to know when the cancel button of the UISearchBar has finished his animation after calling

searchBar.setShowsCancelButton(false, animated: true)

I've tried this

UIView.animateWithDuration(3, animations: {
self.searchBarView.searchBar.showsCancelButton = false
}, completion: {finished in

but the completion block gets triggered immediatly, any solution would be appreciated


You can use layoutIfNeeded() to update view in UIView.animateWithDuration(...)

try this code:

import UIKit

class ViewController: UIViewController {

@IBOutlet var searchBar: UISearchBar!

override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.


override func didReceiveMemoryWarning() {
    // Dispose of any resources that can be recreated.

@IBAction func touchUpInside(sender: UIButton) {
    searchBar.showsCancelButton = !searchBar.showsCancelButton
    UIView.animateWithDuration(3, animations: {
        }, completion: {finished in
            print("Animation finished")

and storyboard: enter image description here