更改 UISearchBar 内 UItextField 中图标的颜色 [英] Changing the color of the icons in a UItextField inside a UISearchBar

查看:42
本文介绍了更改 UISearchBar 内 UItextField 中图标的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义搜索控制器中搜索栏的外观.

I'm trying to customise the appearance of the search bar in my search controller.

设置背景和文本颜色工作正常,但我没有找到改变文本字段中图标颜色的方法,特别是放大镜和 x 按钮.

Setting the background and text colors works fine but I just didn't find a way to change the color of the icons in the text field, specifically the magnifying glass and the x button.

我发现这个 Objective-C 代码应该可以做我想做的事,但我正在努力将它翻译成 Swift:

I've found this Objective-C code which should do what I want but I'm struggling to translate it to Swift:

(跳到工作 Swift 3 解决方案的第一个答案.)

UITextField *searchBarTextField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];

// Magnifying glass icon.
UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView;
leftImageView.image = [LeftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
leftImageView.tintColor = [UIColor whiteColor];

// Clear button
UIButton *clearButton = [searchBarTextField valueForKey:@"_clearButton"];
[clearButton setImage:[clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
clearButton.tintColor = [UIColor whiteColor];

我尝试翻译成 Swift:

My attempt to translate to Swift:

let textField = searchController.searchBar.valueForKey("searchField") as! UITextField
// These two work fine.
textField.backgroundColor = UIColor.blackColor()
textField.textColor = UIColor.blackColor()

var glassIcon = textField.leftView
// This would work.
//glassIcon.hidden = true

// This does not have any effect.
//glassIcon?.tintColor = UIColor.whiteColor()

// My attempt to translate, but it gives an error.
glassIcon.image? = UIImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)

 var clearButton = textField.valueForKey("clearButton")!
            clearButton.setImage(clearButton.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
// This gives the error: "Cannot assign to property: 'clearButton' is immutable
clearButton.tintColor = UIColor.whiteColor()
  // Sorry for the weird formatting, it glitches here in the editor. 

leftView 似乎没有图像属性.如何像 Objective-C 代码那样访问该属性?另外,如果有更好的方法来实现我想要的,请告诉我.

The leftView does not seem to have an image property. How can I access that property as the Objective-C code does? Also, if there is a better to achieve what I want please let me know.

推荐答案

解决方案如下:

// Text field in search bar.
let textField = searchController.searchBar.value(forKey: "searchField") as! UITextField

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

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

这篇关于更改 UISearchBar 内 UItextField 中图标的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆