Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar: changing background color of input field

I'm trying to change the background color of input field of UISearchBar.It's the rounded view where you input text to search, the default color is white. I would like to change it to gray

I tried:

for (UIView *subView in searchBar.subviews) {
    if ([subView isKindOfClass:NSClassFromString(@"UITextField")]) {
        UITextField *textField = (UITextField *)subView;
        [textField setBackgroundColor:[UIColor grayColor]];
    }

But it doesn't work :(

I also tried to insert an image view to TextField but it seems the rounded view is separate from TextField. So, any clues?

like image 428
muc Avatar asked Aug 29 '11 10:08

muc


1 Answers

=)

for (UIView *subView in _searchBar.subviews) {
    for(id field in subView.subviews){
        if ([field isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)field;
            [textField setBackgroundColor:[UIColor grayColor]];
        }
    }
}
like image 169
haruan Avatar answered Nov 05 '22 08:11

haruan