UISearchBar disable auto disable of cancel button


I have implemented a UISearchBar into a table view and almost everything is working except one small thing: When I enter text and then press the search button on the keyboard, the keyboard goes away, the search results are the only items shown in the table, the text stays in the UISearchBar, but the cancel button gets disabled.

I have been trying to get my list as close to the functionality of the Apple contacts app and when you press search in that app, it doesn't disable the cancel button.

When I looked in the UISearchBar header file, I noticed a flag for autoDisableCancelButton under the _searchBarFlags struct but it is private.

Is there something that I am missing when I setup the UISearchBar?

Best Solution

I found a solution. You can use this for-loop to loop over the subviews of the search bar and enable it when the search button is pressed on the keyboard.

for (UIView *possibleButton in searchBar.subviews)
    if ([possibleButton isKindOfClass:[UIButton class]])
        UIButton *cancelButton = (UIButton*)possibleButton;
        cancelButton.enabled = YES;
Related Question