UISearchBar更改占位符颜色

问题描述:

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

+0

我认为这篇文章将帮助你的http://计算器。 com/questions/1340224/iphone-uitextfield-change-placeholder-text-color – frankWhite 2012-08-06 14:52:11

+0

我已经看过它。这篇文章涉及一个UITextfield。子类化UITextfield并重写-drawPlaceholderInRect方法肯定会有所帮助。但是,UISearchBar不是UITextfield的子类,因此我无法重写此方法。 – csotiriou 2012-08-06 20:25:24

+0

对不起,我不知道这个。 UISearchBar是UIView的子类,我认为你可以从它的子类中尝试重写drawRect方法,并在其中添加上一篇文章中的代码。也许这有帮助 – frankWhite 2012-08-07 06:27:08

iOS5+使用外观代理

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

由于我的文章我找到了答案,而且这确实是最好的办法。干净,简单,它不会破坏任何东西。 – csotiriou 2013-07-02 16:46:20

+6

这不是真的有效。 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

+0

只有当我在Stritboard中添加一些占位符时,这才适用于我。直接设置占位符通过代码不工作,如果我们没有在故事板中的一些占位符..有线 – sach 2014-09-12 06:25:02

试试这个:

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

我不想改变颜色正常的文字。我想更改占位符文本的颜色。 – csotiriou 2012-08-06 14:08:24

+4

这不好!当您这样做时,您正在访问PRIVATE iVAR,这可能会导致您的应用在应用商店被拒绝。 – 2014-05-19 11:06:17

+0

@AlexanderW:如果这意味着不能被使用,你将无法访问它...全世界的每个人都使用这个东西来改变textfield占位符颜色变化的颜色....我的应用程序从未被拒绝由于这...但它只是一个解决方案,可能不是一个完美的...... – Ankit 2014-05-23 09:54:06

找到了答案从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"]; 
+0

任何人都试图提交一个应用程序的代码?应该不是什么大问题......但谁肯定可以这么说... – jimpic 2013-04-10 07:17:07

+2

虽然这段代码有效,但我相信任何提交的应用都会违反苹果的规定。 – csotiriou 2013-06-10 17:20:24

+0

我的应用程序可以通过此解决方案提交给appstore。 :) – Senry 2013-08-28 11:31:02

这是一个老的文章,但请查看这篇文章的一个适当的解决方案 iPhone UITextField - Change placeholder text color

试试这个:

[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]; 
+0

这是唯一的解决方案,为我工作正确,其他解决方案在来回导航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; 
     } 
    } 
} 
+0

我对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 
+0

它是公共API吗? – Andrey 2015-07-31 00:43:10

+0

我认为如此。它与其他用户推荐的解决方案相同。 – derdida 2015-07-31 07:08:37

+0

'提示符'怎么样?! (不是占位符) – fatuhoku 2015-11-20 22:25:27

在你的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]; 
+3

我已经从最近的答案中删除了标语 - 在这里的答案中没有用处。谢谢! – Undo 2015-11-16 01:29:54

+0

'[ setValue:forUndefinedKey:]:该类不是关键字_placeholderLabel.textColor的编码兼容密钥值。' – 2016-10-27 05:33:24

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