2017-05-29 107 views
0

請檢查我下面的代碼,我用一個navigationbar這是由一個searchbarsearchbar有一個文本框,現在我想改變的textfield文本顏色和我做了下面的代碼,但它不是工作,所以請幫助糾正它。文本字段的文本顏色變化

[[UISearchBar appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]]setTintColor:[UIColor whiteColor]]; 
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]setTextColor:[UIColor whiteColor]]; 
+0

添加更多的代碼工作。 –

+0

看到這個https://stackoverflow.com/questions/32968907/how-to-change-uisearchbar-textfield-background-color-and-text-color-in-ios8 –

+0

你可以改變文本中的文本的顏色場景從故事板本身。 –

回答

0

如果您使用的是原生UISearchbar控制,那麼你可以改變文字顏色與下面

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

代碼或者你可以通過身份檢查特定searchBar做到這一點: enter image description here

0

試試這個,

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews]; 

for (UIView *view in searchBarSubViews) { 
    if([view isKindOfClass:[UITextField class]]) 
    { 
     UITextField *txt = (UITextField*)view; 
     UIImageView *imgView = (UIImageView*)textField.leftView; 
     imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
     imgView.tintColor = [UIColor whiteColor]; 

     txt.backgroundColor = [UIColor blackColor]; 
     txt.textColor = [UIColor RedColor]; 
     UIButton *btnClear = (UIButton*)[textField valueForKey:@"clearButton"]; 
     [btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; 
     btnClear.tintColor = [UIColor whiteColor]; 
    } 
}