iIS8

2015-10-06 7 views
7

में UISearchBar टेक्स्टफील्ड पृष्ठभूमि रंग और टेक्स्ट रंग को बदलने के लिए कैसे मैंने UISearchBar टेक्स्टफील्ड पृष्ठभूमि रंग बदलने के लिए नीचे कोड का उपयोग किया।iIS8

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

लेकिन यह मेरे लिए काम नहीं करता है, क्या कोई भी समाधान दे सकता है। धन्यवाद अग्रिम में

+0

आप अधिक जानकारी जोड़ सकते हैं "यह * हरिणी मेरे लिए काम नहीं करता * * काफी अस्पष्ट है। – potame

+0

क्या आप uisearchbar में टेक्स्टफील्ड पृष्ठभूमि रंग सेट करने के लिए उदाहरण दे सकते हैं –

उत्तर

17

इस प्रयास करें:

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

बस इस कोड की कोशिश

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

या

प्रयास करें इस

- (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; 
}