Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 UISearchBar cancel button text not showing. Button appears invisible

I am hoping this is an easy question.

enter image description here

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.

enter image description here

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.

like image 268
denvdancsk Avatar asked Oct 21 '13 17:10

denvdancsk


Video Answer


3 Answers

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?

like image 167
in.disee Avatar answered Sep 28 '22 11:09

in.disee


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;
like image 41
Bot Avatar answered Sep 28 '22 11:09

Bot


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.

like image 40
Tekla Keresztesi Avatar answered Sep 28 '22 10:09

Tekla Keresztesi