设置背景和文本颜色工作正常但我没有找到一种方法来更改文本字段中的图标的颜色,特别是放大镜和x按钮.
我发现这个Objective-C代码应该做我想要的但是我很难将它翻译成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 buttonUIbutton *clearbutton = [searchbarTextFIEld valueForKey:@"_clearbutton"];[clearbutton setimage:[clearbutton.imageVIEw.image imageWithRenderingMode:UIImageRenderingModeAlwaystemplate] forState:UIControlStatenormal];clearbutton.tintcolor = [UIcolor whitecolor];
我试图翻译成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 immutableclearbutton.tintcolor = UIcolor.whitecolor() // Sorry for the weird formatting,it glitches here in the editor.
leftVIEw似乎没有图像属性.如何像Objective-C代码那样访问该属性?
此外,如果有更好的实现我想要的,请告诉我.
// Text fIEld in search bar.let textFIEld = searchController.searchbar.value(forKey: "searchFIEld") as! UITextFIEldlet glassIconVIEw = textFIEld.leftVIEw as! UIImageVIEwglassIconVIEw.image = glassIconVIEw.image?.withRenderingMode(.alwaystemplate)glassIconVIEw.tintcolor = UIcolor.whitelet clearbutton = textFIEld.valueForKey("clearbutton") as! UIbuttonclearbutton.setimage(clearbutton.imageVIEw?.image?.withRenderingMode(.alwaystemplate),for: .normal)clearbutton.tintcolor = UIcolor.white总结
以上是内存溢出为你收集整理的swift – 更改UISearchBar中UItextField中图标的颜色全部内容,希望文章能够帮你解决swift – 更改UISearchBar中UItextField中图标的颜色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)