18

I am working on IOS 7 application.By default its appearing like Pic(1).But I need to change it as Pic(2).I googled and found few answers for the requirement,but it has not changed.Or else I need to hide.So that I can manage with background image.This is first image

Pic(1)

enter image description here

I used below code to modify it.But didnt succeed.

In .h file

@property(nonatomic,strong) IBOutlet UISearchBar *findSearchBar;

In .m file

@synthesize findSearchBar;

 - (void)viewDidLoad
 {
[super viewDidLoad];
 [self setSearchIconToFavicon];
 }

 - (void)setSearchIconToFavicon
{
// The text within a UISearchView is a UITextField that is a subview of that UISearchView.
   UITextField *searchField;
   for (UIView *subview in self.findSearchBar.subviews)
   {
       if ([subview isKindOfClass:[UITextField class]]) {
          searchField = (UITextField *)subview;
          break;
      }
  }

if (searchField)
{
    UIView *searchIcon = searchField.leftView;
    if ([searchIcon isKindOfClass:[UIImageView class]])
    {
        NSLog(@"aye");
    }
    searchField.rightView = nil;
    searchField.leftView = nil;
    searchField.leftViewMode = UITextFieldViewModeNever;
    searchField.rightViewMode = UITextFieldViewModeAlways;
}

}

I am not getting how to make the center of the view's image to nil.Its really killing my time.Please help me.where I had gone wrong.

  • Refer for swift-3 : http://stackoverflow.com/questions/19289406/uisearchbar-search-icon-isnot-left-aligned-in-ios7/41976067#41976067 – Shrawan Feb 01 '17 at 09:26

3 Answers3

12
    UITextField *txfSearchField = [looksearchbar valueForKey:@"_searchField"];
    [txfSearchField setBackgroundColor:[UIColor whiteColor]];
    [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
    [txfSearchField setRightViewMode:UITextFieldViewModeNever];
    [txfSearchField setBackground:[UIImage imageNamed:@"searchbar_bgImg.png"]];
    [txfSearchField setBorderStyle:UITextBorderStyleNone];
    //txfSearchField.layer.borderWidth = 8.0f;
    //txfSearchField.layer.cornerRadius = 10.0f;
    txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
    txfSearchField.clearButtonMode=UITextFieldViewModeNever;

Try this may be it will help u........

Hari1251
  • 452
  • 4
  • 15
  • try to set background image instead of setting RGB color to it. – Hari1251 Oct 25 '13 at 04:55
  • 5
    Note that this can get your app rejected from the app store. Specifically the `[looksearchbar valueForKey:@"_searchField"]` part. A safer way is to look through the subviews of the search bar (and the subviews of the subviews as well) until you find the correct subview with `[subview isKindOfClass:[UITextField class]]` – Ray Lillywhite Jan 24 '14 at 22:31
  • 1
    As of iOS 5.0, you can set these values on UITextField via UIAppearance: `[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];` – atticus Apr 12 '14 at 00:24
  • Don't worry about [looksearchbar valueForKey:@"_searchField"] - it's completely okey to do that - no rejections on any of my apps – Grzegorz Krukowski Sep 24 '15 at 11:44
  • @GrzegorzKrukowski, no it isn't ok to do. Anything involving a private API is subject to rejection and is strictly against the rules (regardless if your applications were flagged or not). This is extremely dangerous to even remotely rely on and can change in any incremental update. – TheCodingArt Aug 31 '16 at 17:21
9

I'm not sure how to left-align the placeholder, but as of iOS 5.0 there's a simple, supported way to modify the search bar's text field properties, e.g.:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];

which will hide the magnifying glass icon.

atticus
  • 960
  • 7
  • 10
0

You could try:

searchBar.setImage(UIImage(named: "yourimage")!, forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
David
  • 3,285
  • 1
  • 37
  • 54
DairySeeker
  • 326
  • 3
  • 7