2015-10-06 16 views
7

Użyłem pod kodem do zmiany koloru tła pola tekstowego UISearchBar.jak zmienić kolor tła pola UISearchBar i kolor tekstu w ios8

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{ 
                    NSForegroundColorAttributeName : [UIColor redColor], 
                    NSFontAttributeName : [UIFont systemFontOfSize:15] 
                  }]; 

ale to nie działa dla mnie, może ktoś dać rozwiązanie. góry dzięki

+0

można dodać więcej informacji „* it łania s nie działa dla mnie * "jest dość niejasne. – potame

+0

możesz podać przykład ustawienia koloru tła pola tekstowego w uisearchbar –

Odpowiedz

17

Spróbuj tego:

UITextField *searchField = [self.searchBar valueForKey:@"searchField"]; 

// To change background color 
searchField.backgroundColor = [UIColor blueColor]; 

// To change text color 
searchField.textColor = [UIColor redColor]; 

// To change placeholder text color 
searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Some Text"]; 
UILabel *placeholderLabel = [searchField valueForKey:@"placeholderLabel"]; 
placeholderLabel.textColor = [UIColor grayColor]; 
5

Wystarczy spróbować tego kodu

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor grayColor]]; 

LUB

Spróbuj

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     //change the background color 

[[self searchViewForTextFieldBg:self.searchTextfield] setBackgroundColor:[UIColor grayColor]]; 

//change the textcolor 
self.searchTextfield.textColor =[UIColor greenColor]; 

    } 

    - (UITextField*)searchViewForTextFieldBg:(UIView*)view 
{ 
    if ([view isKindOfClass:[UITextField class]]) { 
     return (UITextField*)view; 
    } 
    UITextField *searchTextField; 
    for (UIView *subview in view.subviews) { 
     searchTextField = [self searchViewForTextFieldBg:subview]; 
     if (searchTextField) { 
      break; 
     } 
    } 
    return searchTextField; 
} 
Powiązane problemy