objective-ccocoa-touchuitextfielduisearchbaruiappearance

Change the borderStyle of the UISearchBar text field


I'm looking for a way to get a UISearchBar with a different background and text color, and no border.

I already did this :

[[[self.searchBar subviews] objectAtIndex:0] setAlpha:0.0];
UITextField* searchField = nil;
for(int i = 0; i < self.searchBar.subviews.count; i++) {
    if([[self.searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
        searchField = [self.searchBar.subviews objectAtIndex:i];
    }
}
if(searchField) {
    searchField.backgroundColor = [UIColor colorWithRed:253.0/255.0 green:242.0/255.0 blue:210.0/255.0 alpha:1.0];
    searchField.textColor = [UIColor colorWithRed:98.0/255.0 green:65.0/255.0 blue:48.0/255.0 alpha:1.0];
    searchField.borderStyle = UITextBorderStyleNone;
}

But I'm looking for a way to change the borderStyle of the UISearchBar's text field. I also want to delete the magnifier button. Is it a way to do theses modifications?


Solution

  • Instead of searching the view hierarchy for the search bar, you could use the appearance API. Have a look under "Customizing Appearance" in the docs.