how to change UISearchBar textfield background color and text color in ios8

前端 未结 2 509
忘了有多久
忘了有多久 2021-02-05 23:14

I used below code for changing UISearchBar textfield background color.

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes         


        
2条回答
  •  无人共我
    2021-02-05 23:54

    Try this:

    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];
    

提交回复
热议问题