ios - 如何在 UIsearchbar iOS swift 中为文本字段添加自定义图像

标签 ios swift uitextfield uisearchbar swift2

我正在尝试创建自定义搜索栏。我想为搜索栏中的文本字段添加自定义图像。我将搜索栏样式设置为最小。我使用以下代码更改背景图像。它不太合适。此导出以编程方式添加

let srchtextfld: UITextField = srchbr.valueForKey("searchField") as! UITextField

    srchtextfld.layer.backgroundColor = UIColor(patternImage: UIImage(named: "Rounded Rectangle 1 copy 3.png")!).CGColor

我正在获取下图

enter image description here

我想添加这种类型的图像作为背景

enter image description here

感谢任何帮助

最佳答案

如果你想设置背景图片那么属性是背景而不是背景颜色见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/

相关文章:

ios - AVCapturesession 不适用于 iOS 中的真实产品条形码

ios - 重新加载数据后未调用 UICollectionViewCell draw() 方法

ios - 在 Swift 2 中使用 Parse 获取 Twitter 个人资料图片

ios - 使用 UIDatePicker 而不是键盘来处理 UITextField 问题

ios - 为什么推送另一个 View Controller 的动画不流畅?

ios - 核心数据 - 如何跟踪实体集的子集

ios - viewDidAppear,还有当应用程序从后台返回时

ios - UITableViewCell 内部的约束问题

objective-c - 在 UITextField 中换行文本?

IOS如何隐藏键盘顶部的顶部栏(带有完成按钮)?