I am hoping this is an easy question.
I have a search bar that shows a cancel button:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[searchBar setShowsCancelButton:YES animated:YES];
}
Only problem is that the text on the cancel button isn't showing.
The button is obviously there because I can click on it but no text appears when the button is shown. Its as if the button is invisible. The code worked fine in iOS6 but now in iOS7 I am having this problem. Any body have any ideas? I am using a UISplitViewController and my search bar is in the navigationItem.titleView of the MasterViewController.
Probably You got clear tint color, it's the only reason I could imaging try to set
_searchBar.tintColor = [UIColor redColor];
How do You create UISearchBar?
I don't know if it is a bug from apple or intended but the cancel button doesn't appear to show while in a navigationController. Try adding the searchbar to a view before adding it to the navigation controller.
UIView *searchBarView = [[UIView alloc] initWithFrame:[searchBar bounds]];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
I had the same problem. The button is there but the titleColor is the same as it's background. What I did to fix it was searching for the cancel button subview of the search bar and set the button's title color to some different color. Be aware to setting this after the cancel button is shown, the view isn't there if before the cancel button is shown. My code:
[self.mySearchBar setShowsCancelButton:YES animated:YES];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
for(id subview in [self.mySearchBar subviews])
{
if ([subview isKindOfClass:[UIButton class]]) {
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
}
}
} else {
for(id subview in [[[self.mySearchBar subviews] objectAtIndex:0] subviews])
{
if ([subview isKindOfClass:[UIButton class]]) {
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
}
}
}
Also note that in iOS 7 the cancel button is buried in the search bar's first subview. Prior iOS 7 it is a direct subview of searchbar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With