Andrea Mario Lufino
Andrea Mario Lufino

Reputation: 7921

UISearchBar cancel button disabled when keyboard disappears on iPad

I have my UISearchBar placed in a navigation bar. When I tap on the bar, it expands itself and shows the cancel button. If you type something in the field and then you tap on "Search" on the keyboard, it dismiss the keyboard showing you the results. But in this moment I'm having a big problem : the cancel button is now disabled (greyed out) and if I tap on it, first the keyboard is opened and then I can tap again on the Cancel to exit from the search. Is it possible to keep the Cancel button active always so I can directly tap on it to exit from the search?

Upvotes: 1

Views: 1010

Answers (1)

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

searchBar.showsCancelButton = YES; 

You can create another custom button on UISearchBar.

for (UIView *subView in searchBar.subviews) {
        if([subView isKindOfClass:[UIButton class]]) {

            UIButton *cancelButton = (UIButton *)[searchBar.subviews lastObject];
            //Make changes to the cancelButton here
            [cancelButton setEnabled:YES];

        }
   }

Upvotes: 1

Related Questions