UISearchBar更改占位符颜色
为iOS5+
使用外观代理
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
由于我的文章我找到了答案,而且这确实是最好的办法。干净,简单,它不会破坏任何东西。 – csotiriou 2013-07-02 16:46:20
这不是真的有效。 UILabel不支持这种定制方式,因为UILabel的'textColor'属性没有按照文档的要求标记为'UI_APPEARANCE_SELECTOR'。另请参见http://*.com/questions/11839044/how-do-i-apply-uiappearance-proxy-properties-to-uilabel – 2013-07-22 02:23:42
只有当我在Stritboard中添加一些占位符时,这才适用于我。直接设置占位符通过代码不工作,如果我们没有在故事板中的一些占位符..有线 – sach 2014-09-12 06:25:02
试试这个:
UITextField *searchField = [searchbar valueForKey:@"_searchField"];
field.textColor = [UIColor redColor]; //You can put any color here.
找到了答案从Change UITextField's placeholder text color programmatically
// 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"];
试试这个:
[self.searchBar setValue:[UIColor whatever] forKeyPath:@"_searchField._placeholderLabel.textColor"];
您还可以设置这故事板,选择搜索栏,在用户自定义运行时添加条目属性:
_searchField._placeholderLabel.textColor
型彩色的
,并选择你需要的颜色。
第一个解决方案是可以的,但是如果你使用多个UISearchBar,或者创建了很多实例,它可能会失败。在一个解决方案,总是对我的工作是也使用外观代理,但直接的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];
这是唯一的解决方案,为我工作正确,其他解决方案在来回导航viewController层次结构时失败,并且销毁/重新创建搜索栏 – DTs 2016-12-17 22:46:20
后调查了几个答案,我出来这一点,希望其帮助
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;
}
}
}
我对UISearchBar类进行了子类化,然后使用了与您一样的属性占位符。但在我的子类中,我添加了override var placeholder:String? {0} {0} {0} {0}这样,无论何时我改变此searchBar上的占位符文本,它都会自动使占位符文本成为正确的颜色。如果我没有这样做,那么如果我设置了一个普通的占位符,它会覆盖我的占位符颜色。 – 2017-01-29 01:59:44
这里是一个解夫特:
夫特2
var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
var textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor.whiteColor()
斯威夫特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
在你的viewController中的viewDidLoad将这个(iOS上的9个工作):
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];
我已经从最近的答案中删除了标语 - 在这里的答案中没有用处。谢谢! – Undo 2015-11-16 01:29:54
'[
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
}
斯威夫特3
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.white
该解决方案适用于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:
[_searchBar setImage:[UIImage imageNamed:@"your-image-name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
关于占位符文本颜色,你可以检查我的其他的答案,它采用了分类,在这里:UISearchBar change placeholder color
我认为这篇文章将帮助你的http://计算器。 com/questions/1340224/iphone-uitextfield-change-placeholder-text-color – frankWhite 2012-08-06 14:52:11
我已经看过它。这篇文章涉及一个UITextfield。子类化UITextfield并重写-drawPlaceholderInRect方法肯定会有所帮助。但是,UISearchBar不是UITextfield的子类,因此我无法重写此方法。 – csotiriou 2012-08-06 20:25:24
对不起,我不知道这个。 UISearchBar是UIView的子类,我认为你可以从它的子类中尝试重写drawRect方法,并在其中添加上一篇文章中的代码。也许这有帮助 – frankWhite 2012-08-07 06:27:08