我正在尝试创建自定义搜索栏。我想为搜索栏中的文本字段添加自定义图像。我将搜索栏样式设置为最小。我使用以下代码更改背景图像。它不太合适。此导出以编程方式添加
let srchtextfld: UITextField = srchbr.valueForKey("searchField") as! UITextField
srchtextfld.layer.backgroundColor = UIColor(patternImage: UIImage(named: "Rounded Rectangle 1 copy 3.png")!).CGColor
我正在获取下图
我想添加这种类型的图像作为背景
感谢任何帮助
最佳答案
如果你想设置背景图片那么属性是背景而不是背景颜色见docs .所以你喜欢这样
srchtextfld.background = UIImage(named: "Rounded Rectangle 1 copy 3.png")!
还记得
When set, the image referred to by this property replaces the standard appearance controlled by the borderStyle property. Background images are drawn in the border rectangle portion of the text field. Images you use for the text field’s background should be able to stretch to fit.
加上
UITextBorderStyleRoundedRect, custom background images are ignored.
关于ios - 如何在 UIsearchbar iOS swift 中为文本字段添加自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33034402/