0

In my iPhone app I am trying to set background image to UISearch Bar, my image is not getting displayed.

Here is my code

UISearchBar * tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake( 0, 0, self.view.bounds.size.width, 40 )];

self.searchBar = tempSearchBar;
tempSearchBar.delegate = self; 
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.barStyle = UIBarStyleBlack;
self.searchBar.backgroundColor=[UIColor clearColor];
self.searchBar.backgroundImage=[UIImage imageNamed:@"Searchbox.png"];
self.searchBar.placeholder = @"Search My Data";
[self.searchBar sizeToFit];  

[self.view addSubview:self.searchBar];
[tempSearchBar release];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nishi
  • 683
  • 1
  • 12
  • 31
  • http://stackoverflow.com/questions/14134443/make-uitextfield-to-function-like-uisearchbar/14134492#14134492 – Murali Mar 04 '13 at 05:57
  • [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"inputbox.png"] forState:UIControlStateNormal]; – Abdul Yasin Sep 03 '13 at 09:24

5 Answers5

8
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"Searchbox.png"] forState:UIControlStateNormal];

By using above method I can set bg Image for searchBar

Nishi
  • 683
  • 1
  • 12
  • 31
2
[self.searchDisplayController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_bar"] forState:UIControlStateNormal];

While Interface Builder provides the equivalent functionality of the following code (not the desired effect though)

[self.searchDisplayController.searchBar setBackgroundImage:[UIImage imageNamed:@"search_bar"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
zeeawan
  • 6,667
  • 2
  • 50
  • 56
0
  • (void) clearSearchBarBg {

for (UIView *subview in searchBarCity.subviews) {

    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
    {
        [subview removeFromSuperview];
        break;
    }

}

for (UIView *subview in searchBarCity.subviews)
{

    if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
    {
        [(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever];
    }
}
for(UIView *subView in searchBarCity.subviews)
{

    if([subView isKindOfClass: [UITextField class]])
    {
        [(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceAlert];
    }
}

UITextField *searchField = [searchBarCity valueForKey:@"_searchField"];
[searchField setBackground:[UIImage imageNamed:@"SearchInputBox.png"]];// = [UIColor clearColor];

searchField.borderStyle = UITextBorderStyleNone;
[searchField setTextColor:[UIColor blackColor]];

UIImageView* image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
image.image = [UIImage imageNamed:@"navigationBarColorCode.png"];
image.userInteractionEnabled = FALSE;
[searchBarCity insertSubview:image belowSubview:searchField];

searchBarCity.userInteractionEnabled = YES;
searchBarCity.placeholder = @"Search";

}

Napster
  • 120
  • 6
0

Use below code

[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"] forState:UIControlStateNormal]; 
self.searchDisplayController.searchBar.barTintColor = [UIColor clearColor];
self.searchBar.tintColor=[UIColor orangeColor];
Venkat Reddy
  • 111
  • 1
  • 8
0

Set the background image of the SearchBar:

[self.searchController.searchBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

then change SearchField Background Image:

[self.searchController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"Searchbox.png"] forState:UIControlStateNormal];

If you don't want to show the Search Icon in the search bar then do the following:

UITextField *searchField = (UITextField *)[self.searchController.searchBar valueForKey:@"searchField"];
searchField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LEFT_VIEW_WIDTH, searchField.frame.size.height)];
Torongo
  • 1,021
  • 1
  • 7
  • 14