Ezi00 Ezi00 - 1 year ago 125
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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download