【问题标题】:Change UISearchbar Magnifying Glass Color, PlaceHolder Color, and X Color更改 UISearchbar 放大镜颜色、PlaceHolder 颜色和 X 颜色
【发布时间】:2016-04-01 02:25:54
【问题描述】:

我已经搜索了各种解决方案来完成这项任务,但它们要么在目标 C 中,要么涉及更换放大镜图像。

我看过的以前的帖子:

Change color of magnifying glass

How to change UISearchBar Placeholder and image tint color?

我不想替换放大镜图像的原因是因为页面颜色是动态的,并且有超过 100 多种颜色组合

任何关于更改 UISearchbar 放大镜颜色、PlaceHolder 颜色和 X 颜色的帮助将不胜感激

【问题讨论】:

  • 你看过那些帖子了吗?忘记替换图像。占位符颜色和色调颜色呢?你有没有尝试过?
  • 你看过这个教程了吗:appcoda.com/custom-search-bar-tutorial
  • @Mr.T 我已经替换了 tintcolor,它改变了输入文本和取消按钮的字体颜色,但不是占位符或 X 按钮。
  • 你看过的一篇文章建议了一种改变占位符颜色的方法,你试过吗?
  • @Mr.T 我让占位符 textcolor 使用属性文本属性进行更改。我仍在研究如何更换 X 和放大镜

标签: ios swift uisearchbar uicolor


【解决方案1】:

Shahil 为 Swift 3 更新了答案:

let textField = searchBar.value(forKey: "searchField") as! UITextField

let glassIconView = textField.leftView as! UIImageView
glassIconView.image = glassIconView.image?.withRenderingMode(.alwaysTemplate)
glassIconView.tintColor = .white


let clearButton = textField.value(forKey: "clearButton") as! UIButton
clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
clearButton.tintColor = .white

【讨论】:

    【解决方案2】:

    目标 C:

    NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
    for (UIView *view in searchBarSubViews) {
        if([view isKindOfClass:[UITextField class]])
        {
            UITextField *textField = (UITextField*)view;
            UIImageView *imgView = (UIImageView*)textField.leftView;
            imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            imgView.tintColor = [UIColor whiteColor];
    
            UIButton *btnClear = (UIButton*)[textField valueForKey:@"clearButton"];
            [btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
            btnClear.tintColor = [UIColor whiteColor];
    
        }
    }
    [self.searchBar reloadInputViews];
    

    斯威夫特:

    // Text field in search bar.
    let textField = searchController.searchBar.valueForKey("searchField") as! UITextField
    
    let glassIconView = textField.leftView as! UIImageView
    glassIconView.image = glassIconView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    glassIconView.tintColor = UIColor.whiteColor()
    
    let clearButton = textField.valueForKey("clearButton") as! UIButton
    clearButton.setImage(clearButton.imageView?.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal)
    clearButton.tintColor = UIColor.whiteColor()
    

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多