UISearchBar in UIToolbar will not show cancel button

I have a UISearchBar nested in a UIBarButtonItem in a UIToolbar. The main functionality works, but the cancel button and visibility bar are not displayed. I tried to enable them in the interface builder, and I also tried manually calling [searchBar setShowsCancelButton:YES], but none of them work. Any ideas?

This is on an iPad. I tried in iOS 3 and it also does not work in 4.2.

+5
source share
3 answers

You need to wrap the UISearchBar with another view.

UISearchBar *searchBar = [UISearchBar new];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:searchBar.frame];
[searchBarContainer addSubview:searchBar];
UIBarButtonItem *searchBarItem =
    [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
+11
source

, UISearchBar. UISearchBar UIToolbar, , . , , - .

+3

Do I really need to use the "Cancel" button in the search bar? Just use the textDidChange method to see if the line has been cleared, and use this to do the same as undo.

0
source

All Articles