Last Updated: September 29, 2021
·
5.527K
· mystcolor

Customizing UISearchBar background

There're few attempts out there including subclass UISearchBar and hack it's layoutSubviews or drawLayer:. After iOS 5, the best method is to use UIAppearance.

// Configure your images
UIImage *backgroundImage = [UIImage imageNamed:@"searchbar"];
UIImage *searchFieldImage = [[UIImage imageNamed:@"searchfield"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

// Set it to your UISearchBar appearance
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];