5

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.

denvdancsk
  • 3,033
  • 2
  • 26
  • 41

6 Answers6

9

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?

Bot
  • 11,868
  • 11
  • 75
  • 131
in.disee
  • 1,102
  • 1
  • 7
  • 15
  • I thought that might be the issues but I tried different tint colors and also removing the tint completely and it didn't make a difference. I create the search bar programatically. It's inserted into a UIView which is added to the navigationItem.titleView of the MasterViewController. – denvdancsk Oct 21 '13 at 21:37
  • I had an issue with my background image but this also helped. – denvdancsk Jan 27 '14 at 19:51
5

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;
Bot
  • 11,868
  • 11
  • 75
  • 131
  • I already tried that but it didn't work. All that does is add the button without animation but it's still invisible. I create the search bar programatically so there's no outlets to connect only the delegate which is assigned correctly because the delegate methods are all working. – denvdancsk Oct 21 '13 at 21:38
  • 1
    @kyzer1978 i missed the part about it being in a navigationItem. I updated my answer – Bot Oct 21 '13 at 21:49
  • Thanks. I tried that as well with no luck. I don't think putting it in the navigation control is the problem because there's another search bar in my app on a different tableViewController that has the same problem but it's in the tableview header instead of the navigation controller. – denvdancsk Oct 22 '13 at 00:49
  • 1
    @kyzer1978 can you create a test project and upload it somewhere? – Bot Oct 22 '13 at 16:03
  • I just figured it out. It had something to do with the background image I was using. Thanks for the help though. – denvdancsk Oct 24 '13 at 21:38
3

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.

-1

This is not a bug,Please make sure the tint color is visible with your background color.enter image description here

Vicky
  • 1,095
  • 16
  • 15
-1

_searchBar.backgroundImage = <#Some Color#>; _searchBar.backgroundColor = <#Some Color#>; // Cancel Button Color.

Zeeshan
  • 4,194
  • 28
  • 32
-1

By going through above answers, I was unable to figure out the correct answer. As this is very old question, but if someone come across this issue and they using Storybaord, there is a option available in Storybaord (i.e click on UISearchbar , then go to Attribute Inspector and in Options look for "Shows Cancel Button") . Please refer the screenshotenter image description here

Nilesh Kumar
  • 2,141
  • 3
  • 16
  • 20