Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change inside background color of UISearchBar component on iOS

I know how to remove/change UISearchBar background color around search field:

[[self.searchBar.subviews objectAtIndex:0] removeFromSuperview]; self.searchBar.backgroundColor = [UIColor grayColor]; 

achieved UISearchBar customization

But don't know how to do this inside it like that:

desired UISearchBar customization

This needs to be compatible with iOS 4.3+.

like image 328
Borut Tomazin Avatar asked Dec 11 '12 09:12

Borut Tomazin


People also ask

How do I change the color of my UISearchBar in Swift?

Change Search Bar Default Image Color The left hand default search image in UISearchBar represents the left view of the UITextField. The Image is rendered to change it to the desired colour. @IBOutlet weak var searchBar: UISearchBar! Hope it will help you in customising the UISearchBar in your app.

How do I change the background color of a view controller in Xcode?

Find the view or view controller you want to change the background color of. Open it up in the interface builder and open the Attributes Inspector. Find the background color field and set it to the color you want.

How do I use UISearchBar?

You can add a UISearchBar as you would with any other control by dragging one to your view controller in interface builder or by programmatically adding it. The delegate property of search bar must be set to an object that implements UISearchBarDelegate.


Video Answer


1 Answers

Just customize the text field itself.

I am simply doing this and it works fine for me (iOS 7).

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"]; txfSearchField.backgroundColor = [UIColor redColor]; 

This way you don't need to create an image, size it, etc...

like image 68
AbuZubair Avatar answered Oct 06 '22 09:10

AbuZubair