Cancel Button in UISearchController

Swift 5

searchBar.delegate = self
.......

extension YourClass: UISearchBarDelegate {
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar){} 
}

You need to set the UISearchController searchBar's delegate. Once you have done this, the addition of the delegate method searchBarCancelButtonClicked: will properly be called.

self.searchController.searchBar.delegate = self;

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
}

If you implement UISearchResultsUpdating protocol, you can know that cancelled is triggered when active is false.

func updateSearchResultsForSearchController(searchController: UISearchController) {
    if !searchController.isActive {
        print("Cancelled")
    }
}