I found a different approach for it to work in iOS 7.
What I'm trying is similar to the Twitter iOS app. If you click on the magnifying glass on the Timelines tab, a UISearchBar appears with the Cancel button activated, a keyboard and the last search screen. Scroll through the last search screen and it hides the keyboard, but it holds the Cancel button.
This is my working code:
UIView *searchBarSubview = self.searchBar.subviews[0]; NSArray *subviewCache = [searchBarSubview valueForKeyPath:@"subviewCache"]; if ([subviewCache[2] respondsToSelector:@selector(setEnabled:)]) { [subviewCache[2] setValue:@YES forKeyPath:@"enabled"]; }
I came to this solution by setting a breakpoint in my scrollViewWillBeginDragging: table scrollViewWillBeginDragging: . I looked into my UISearchBar and exposed its subtitles. It always has only one that is of type UIView (my searchBarSubview variable).

Then, that UIView contains an NSArray called subviewCache , and I noticed that the last third element is of type UINavigationButton , and not a public API. Therefore, I decided to use the encoding with the key. I checked if the UINavigationButton responds to setEnabled: and, fortunately, it does. So I set the @YES property. It turns out that UINavigationButton is the Cancel button.
It will definitely break if Apple decides to change the implementation of the UISearchBar internally, but what the hell. He is working now.
Matt Quiros Feb 25 '14 at 17:26 2014-02-25 17:26
source share