Search code examples
iphoneiosuisearchbaruisearchdisplaycontroller

How to color a UISearchBar so that it looks seamless with the background


I'm trying to color this UISearchBar so that it looks seamless with the background (like in the second image). Is it possible to do this? I've tried setting in IB and programmatically but no luck.

thanks for any help

UISearchBar screenshot

Here's the desired look:

UISearchBar screenshot - desired look


Solution

  •    UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
        [searchField setBackground:[UIImage imageNamed:@"searchfield_bg"]];
    
        [self.searchBar setTintColor:[UIColor blackColor]];
        UIImage *searchimg = [UIImage imageNamed:@"search_panel.png"];
    
        for (UIView *subview in self.searchBar.subviews) {
            if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
                bg.backgroundColor = [UIColor colorWithPatternImage:searchimg];
                [self.searchBar insertSubview:bg aboveSubview:subview];
                [subview removeFromSuperview];
                [bg release];
                break;
            }
        }
    

    Im using these images, you can use your and change the color too, my requirements were to make a black searchbar so im using black color

    enter image description here enter image description here

    and it will look like: enter image description here