1

I'm trying to customise the UISearchBarin my current app without much success. What I am trying to do is this:

enter image description here

However the end result is this:

enter image description here

You see in the second image I still have the original search icon there. I have not been able to find a way to remove it all together. I thought setting a custom background image would solve it, but evidently, not.

Edit: The suggested duplicate question mentioned doesn't solve the problem. Non the provided code works in removing the icon.

Edit 2:

This is how I have added the background image, which contains the orange icon on the left

    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_bar"] forState:UIControlStateNormal];
halfer
  • 19,824
  • 17
  • 99
  • 186
Robert J. Clegg
  • 7,231
  • 9
  • 47
  • 99

2 Answers2

1

Even though the answer exists HERE, it does not work with iOS >= 7.x. The following will:

for (UIView *subView in self.searchedBar.subviews) {

        for (UIView *subView2 in subView.subviews) {

            if ([NSStringFromClass([subView2 class]) isEqualToString:@"UISearchBarTextField"]) {

                UITextField *searchField = (UITextField *)subView2;
                searchField.leftViewMode = UITextFieldViewModeNever;
            }
        }
    }
Community
  • 1
  • 1
n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60
1

The following works for me on iOS 8+:

[mySearchBar setImage:[UIImage imageNamed:@"SearchBarIcon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
edwardmp
  • 6,339
  • 5
  • 50
  • 77