UISearchBar更改占位符颜色

有没有人有任何想法或代码示例如何更改UISearchBar的占位符文本的文本颜色?

对于iOS5+使用外观代理

 [[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]]; 

通过编程方式find更改UITextField的占位符文本颜色的答案

 // Get the instance of the UITextField of the search bar UITextField *searchField = [searchBar valueForKey:@"_searchField"]; // Change search bar text color searchField.textColor = [UIColor redColor]; // Change the search bar placeholder text color [searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"]; 

第一个解决scheme是可以的,但是如果你使用多个UISearchBar,或者创build很多实例,它可能会失败。 总是为我工作的一个解决scheme是也使用外观代理,但直接在UITextField

  NSDictionary *placeholderAttributes = @{ NSForegroundColorAttributeName: [UIColor darkButtonColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15], }; NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.searchBar.placeholder attributes:placeholderAttributes]; [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setAttributedPlaceholder:attributedPlaceholder]; 

这是Swift的解决scheme:

Swift 2

 var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField textFieldInsideSearchBar?.textColor = UIColor.whiteColor() var textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel textFieldInsideSearchBarLabel?.textColor = UIColor.whiteColor() 

Swift 3

 let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField textFieldInsideSearchBar?.textColor = UIColor.white let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel textFieldInsideSearchBarLabel?.textColor = UIColor.white 
 if let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as ? UITextField { textFieldInsideSearchBar ? .textColor = UIColor.white if let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as ? UILabel { textFieldInsideSearchBarLabel ? .textColor = UIColor.white if let clearButton = textFieldInsideSearchBar ? .value(forKey: "clearButton") as!UIButton { clearButton.setImage(clearButton.imageView ? .image ? .withRenderingMode(.alwaysTemplate), for : .normal) clearButton.tintColor = UIColor.white } } let glassIconView = textFieldInsideSearchBar ? .leftView as ? UIImageView glassIconView ? .image = glassIconView ? .image ? .withRenderingMode(.alwaysTemplate) glassIconView ? .tintColor = UIColor.white } 

尝试这个:

 [self.searchBar setValue:[UIColor whatever] forKeyPath:@"_searchField._placeholderLabel.textColor"]; 

您还可以在故事板中设置此项,selectsearch栏,在用户定义的运行时属性下添加条目:

 _searchField._placeholderLabel.textColor 

types的颜色,并select你需要的颜色。

这是一个旧的职位,但请检查这个职位的适当的解决schemeiPhone的UITextField – 更改占位符的文字颜色

把它放在viewController的viewDidLoad(在iOS9上工作):

 UITextField *searchField = [self.searchBar valueForKey:@"_searchField"]; //self.searchBar is mine targeted searchBar, replace with your searchBar reference // Change search bar text color searchField.textColor = [UIColor whiteColor]; // Change the search bar placeholder text color [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; //Change search bar icon if needed [self.searchBar setImage:[UIImage imageNamed:@"searchIcon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

经过调查了几个答案,我出来这个,希望它的帮助

 for (UIView *subview in searchBar.subviews) { for (UIView *sv in subview.subviews) { if ([NSStringFromClass([sv class]) isEqualToString:@"UISearchBarTextField"]) { if ([sv respondsToSelector:@selector(setAttributedPlaceholder:)]) { ((UITextField *)sv).attributedPlaceholder = [[NSAttributedString alloc] initWithString:searchBar.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}]; } break; } } } 

Swift 3

 UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.white 

这个解决scheme在Xcode 8.2.1上工作。 与Swift 3.0。 :

 extension UISearchBar { func setPlaceholderTextColorTo(color: UIColor) { let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField textFieldInsideSearchBar?.textColor = color let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel textFieldInsideSearchBarLabel?.textColor = color } } 

用法示例:

 searchController.searchBar.setPlaceholderTextColorTo(color: mainColor) 

这是一个古老的问题,但是现在任何人都在磕磕绊绊,您可以使用以下方式更改iOS 8.x – 10.3上的search图标:

 [_searchBar setImage:[UIImage imageNamed:@"your-image-name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

关于占位符文本的颜色,你可以检查我的其他答案,它使用一个类别,在这里: UISearchBar改变占位符的颜色

尝试这个:

  UITextField *searchField = [searchbar valueForKey:@"_searchField"]; field.textColor = [UIColor redColor]; //You can put any color here.