1

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?

Andrea Mario Lufino
  • 7,921
  • 12
  • 47
  • 78
  • Possible duplicate of [UISearchBar disable auto disable of cancel button](http://stackoverflow.com/questions/4348351/uisearchbar-disable-auto-disable-of-cancel-button) – Nicolai Henriksen Mar 20 '17 at 19:24

1 Answers1

1
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];

        }
   }
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
  • 1
    Yes, in the end I did in this way. But I have the feeling that it's not a clean way. And, another question is : why the cancel button disable itself in that specific situation? – Andrea Mario Lufino Nov 11 '16 at 15:37