简体   繁体   English

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

[英]How to add a custom image for the textfield in UIsearchbar iOS swift

I am trying to create a custom search bar. 我正在尝试创建自定义搜索栏。 I want to add a custom image for the textfield in the search bar. 我想在搜索栏中为文本字段添加自定义图像。 I set the searchbarstyle minimal. 我将searchbarstyle设置为最小。 I used the following code to change the background image. 我使用以下代码更改了背景图片。 it doesn't fit well. 它不太适合。 This outlets are added programmatically 该出口以编程方式添加

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

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

I am Getting the below image 我正在获取下图

在此处输入图片说明

I want to add this type of image as its background 我想添加这种类型的图像作为背景

在此处输入图片说明

Any help is appreciated 任何帮助表示赞赏

If you want to set the background image then property is background not backgroundColor see docs . 如果要设置背景图像,则属性为background而不是backgroundColor,请参阅docs So you do like this 所以你喜欢这样

srchtextfld.background  = UIImage(named: "Rounded Rectangle 1 copy 3.png")!

Also remember 还记得

When set, the image referred to by this property replaces the standard appearance controlled by the borderStyle property. 设置后,此属性引用的图像将替换由borderStyle属性控制的标准外观。 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. 用于文本字段背景的图像应能够拉伸以适合。

Plus

UITextBorderStyleRoundedRect, custom background images are ignored. UITextBorderStyleRoundedRect,将忽略自定义背景图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM