Custom UISearchController Animation Custom UISearchController Animation objective-c objective-c

Custom UISearchController Animation


Subclass UISearchController and implement the optional UIViewControllerTransitioningDelegate method - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;

to return your animation object to do the animation for the dismiss.


I cannot claim that this produces super smooth animation, but it doesn't look terrible and might be helpful (or even exactly what you need). If you want to try it out, you can create a new project (single view application) and just add navigation controller as the initial one and object of class ViewController as its root controller (just to quickly get the navigation bar). Code below is my whole implementation of ViewController

import UIKitclass ViewController: UIViewController,UISearchBarDelegate,UISearchControllerDelegate,UISearchResultsUpdating {    lazy var createMoxyButton:UIBarButtonItem = {        //customize this as your equire        let button = UIBarButtonItem(title: "Done", style: .Plain, target: nil, action: nil)        return button    }()    lazy var centerMapButton:UIBarButtonItem = {        //customize this as your equire        let button = UIBarButtonItem(title: "Done", style: .Plain, target: nil, action: nil)        return button        }()    lazy var searchController:UISearchController = {        let controller = UISearchController(searchResultsController: self)        controller.delegate = self        controller.searchResultsUpdater = self        controller.dimsBackgroundDuringPresentation = false        controller.hidesNavigationBarDuringPresentation = false        return controller    }()    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        self.navigationItem.titleView = self.searchController.searchBar        self.navigationItem.rightBarButtonItems = [self.centerMapButton,self.createMoxyButton]        self.edgesForExtendedLayout = UIRectEdge.None        self.searchController.searchBar.delegate = self    }    func updateSearchResultsForSearchController(searchController: UISearchController) {    }    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {        //this needs to be done because in shouldEndEditing        //we need to set it to false to smooth animation        searchBar.showsCancelButton = true        self.navigationItem.setRightBarButtonItems(nil, animated: true)    }    func searchBarShouldEndEditing(searchBar: UISearchBar) -> Bool {        searchBar.showsCancelButton = false        self.navigationItem.rightBarButtonItems = [self.centerMapButton,self.createMoxyButton]        return true    }}