Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize appearance of UISearchBar

I want to customize the search bar, I mean the white box. Can I do it? Is there some documentation about that? Is there a way to hide the white box, but not the letters. Can I at least make the box smaller? I mean less height

like image 333
Oscar Avatar asked Jun 01 '11 12:06

Oscar


People also ask

How do I customize the search bar 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.


2 Answers

By IOS 5.0 you have the chance to use

  [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbar.png"]forState:UIControlStateNormal]; 
like image 143
Ilker Baltaci Avatar answered Sep 24 '22 06:09

Ilker Baltaci


Here is the solution I was looking for: Subclassing a UISearchBar, & overwriting the method layoutSubviews

- (void)layoutSubviews {    UITextField *searchField;    NSUInteger numViews = [self.subviews count];    for(int i = 0; i < numViews; i++) {       if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?         searchField = [self.subviews objectAtIndex:i];       }    }    if(!(searchField == nil)) {        searchField.textColor = [UIColor whiteColor];        [searchField setBackground: [UIImage imageNamed:@"buscador.png"] ];        [searchField setBorderStyle:UITextBorderStyleNone];    }     [super layoutSubviews]; } 
like image 34
Oscar Avatar answered Sep 26 '22 06:09

Oscar