6

I tried:

UITextField *searchtextfield = [searchBar.subviews objectAtIndex:1];
    UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cButton.frame = CGRectMake(0, 0, 20 , 20);
    cButton.backgroundColor = [UIColor clearColor];
    [cButton setImage:[UIImage imageNamed:@"x-button"] forState:UIControlStateNormal];//your button image.
    cButton.contentMode = UIViewContentModeScaleToFill;
    [cButton addTarget:self action:@selector(xButtonPressed) forControlEvents:UIControlEventTouchUpInside];//This is the custom event
    [searchtextfield setRightView:cButton];
    [searchtextfield setRightViewMode:UITextFieldViewModeWhileEditing];

But it doesn't work very well... It displays custom button, but when you type something, the old one get's back...

How can I do this?

1337code
  • 385
  • 1
  • 6
  • 19

6 Answers6

8

If you want to set a custom clear button in a UISearchBar try this:

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"MyClearButton.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];

Don't forget to set an image for UIControlStateHighlighted

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"HighlightedClearButton.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
Dani
  • 4,551
  • 2
  • 30
  • 48
irblue
  • 841
  • 7
  • 8
  • Thanks, works perfectly. If someone is looking for the Swift version of this code, it can be found there: http://stackoverflow.com/a/25419881/1447641 – Apfelsaft Aug 21 '14 at 06:54
  • It worked for me. Thanks. I thought I had to do it with the UISearchBar object instance. – Augusto Carmo Jun 14 '17 at 11:51
7

You need the following

[searchBar setImage:[UIImage imageNamed:@"image1"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
[searchBar setImage:[UIImage imageNamed:@"image2"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];

It is strongly recommended to place strings in this order starting from UIControlStateHighlighted in case you want to use the same image: image1=image2=image.

In iOS7 it is weird but fact that direct order of UIControlStateNormal and UIControlStateHighlighted doesn't work.

malex
  • 9,690
  • 3
  • 55
  • 76
0

You can hide your cancel button on searchBarTextDidBeginEditing

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}  

And the most amazing you can also hide you clear button by

UITextField *textField=(UITextField*)[[searchBar subviews]objectAtIndex:1];
textField.clearButtonMode=UITextFieldViewModeNever; 

Follow my answer foe more info link

Community
  • 1
  • 1
Rajneesh071
  • 30,084
  • 13
  • 60
  • 74
0

Set clearButtonMode to UITextFieldViewModeNever and rightViewMode to UITextFieldViewModeAlways

Rajneesh071
  • 30,084
  • 13
  • 60
  • 74
Mert
  • 5,945
  • 3
  • 20
  • 32
0

In swift 2.2, following code worked for me

        [UISearchBar .appearance().setImage(UIImage(named: "search_clear_icon"), forSearchBarIcon: .Clear, state: .Normal)]

    [UISearchBar .appearance().setImage(UIImage(named: "search_clear_icon"), forSearchBarIcon: .Clear, state: .Highlighted)]
Mohsin Qureshi
  • 1,173
  • 2
  • 14
  • 25
0

Swift 4.2, 4.1+ of malex answer,

UISearchBar.appearance().setImage(UIImage(named: "image1"), for: .clear, state: .normal)
UISearchBar.appearance().setImage(UIImage(named: "image2"), for: .clear, state: .highlighted)

I also answered this here to set clear button tintColor along with results button customization.

Kamran
  • 14,386
  • 3
  • 30
  • 47