2015-10-06 29 views
7

Tôi đã sử dụng mã bên dưới để thay đổi màu nền của trường văn bản UISearchBar.cách thay đổi màu nền của trường văn bản UISearchBar và màu văn bản trong ios8

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

nhưng nó không hoạt động đối với tôi, bất cứ ai có thể đưa ra giải pháp. cảm ơn trước

+0

Bạn có thể bổ sung thêm thông tin "* nó doe s không làm việc cho tôi * "là khá mơ hồ. – potame

+0

bạn có thể đưa ra ví dụ để thiết lập màu nền của trường text trong uisearchbar –

Trả lời

17

Hãy thử điều này:

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

Chỉ cần thử mã này

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

HOẶC

Hãy thử điều này

- (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; 
} 
Các vấn đề liên quan