将图像添加到 Xcode 中的 UITextField?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13508836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:38:58  来源:igfitidea点击:

Add image to UITextField in Xcode?

iphonexcodeuitextfielduipickerview

提问by Honey

enter image description here

在此处输入图片说明

I need to implement the concept of dropdownin Xcode.

我需要dropdown在 Xcode 中实现 的概念。

For that purpose, I'm using a UIPickerview.

为此,我正在使用UIPickerview.

This pickerView loads when a textfield is tapped (on textFieldDidBeginEditing:)event.

当点击文本字段时(在 textFieldDidBeginEditing :) 事件上加载此 pickerView。

Question:

题:

Is there a way, that I can add a image to TextField?

有没有办法,我可以添加图像TextField

The image is like an arrow mark by which user can understand that a dropdownappears when textfieldis tapped .How can I make it?

图像就像一个箭头标记,用户可以通过它来理解点击dropdowntextfield会出现a。我该如何制作?

回答by Mayur Birari

UITextField has a rightViewproperty, if you have image like this- enter image description herethen You can easly set ImageView object to rightView:

UITextField 有一个rightView属性,如果你有这样的图像 -在此处输入图片说明那么你可以很容易地将 ImageView 对象设置为 rightView:

UITextField *myTextField = [[UITextField alloc] init];

myTextField.rightViewMode = UITextFieldViewModeAlways;
myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]];

回答by King-Wizard

In Swift:

在斯威夫特:

textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = UIImageView(image: UIImage(named: "imageName"))

回答by Erhan Demirci

You can put the UIImageViewto the left of your UITextField.

你可以把UIImageView你的左边UITextField

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(96, 40, 130, 20)];
[textField setLeftViewMode:UITextFieldViewModeAlways];
textField.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIccon.png"]];

回答by Sourabh Sharma

Swift 3.0

斯威夫特 3.0

txtField.rightViewMode = .always
txtField.rightView = UIImageView(image: UIImage(named: "selectdrop"))

TextField With DropDown Icon

带有下拉图标的文本字段

回答by kishorer747

To add proper margins / paddings between image and textfield, try this below code. Expanding on @King-Wizard's answer.

要在图像和文本字段之间添加适当的边距/填充,请尝试以下代码。扩展@King-Wizard 的回答。

Here the height of my TextField is 44 Check the attached image for reference.

这里我的 TextField 的高度是 44 检查附加的图像以供参考。

Swift Version:

迅捷版:

emailTF.leftViewMode = .Always
let emailImgContainer = UIView(frame: CGRectMake(emailTF.frame.origin.x, emailTF.frame.origin.y, 40.0, 30.0))
let emailImView = UIImageView(frame: CGRectMake(0, 0, 25.0, 25.0))
emailImView.image = UIImage(named: "image1")
emailImView.center = emailImgContainer.center
emailImgContainer.addSubview(emailImView)
emailTF.leftView = emailImgContainer

enter image description here

在此处输入图片说明

回答by Zaid Pathan

Step 1:Add this class to your project and add it to your textFiled's class in identity inspector,

第 1 步:将此类添加到您的项目中,并将其添加到身份检查器中的 textFiled 类中,

class MyCustomTextField: UITextField {

    @IBInspectable var inset: CGFloat = 0

    @IBInspectable var leftImage: String = String(){
        didSet{
            leftViewMode = UITextFieldViewMode.Always
            leftView = UIImageView(image: UIImage(named: leftImage))
        }
    }

    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, inset, inset)
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return textRectForBounds(bounds)
    }

    override func leftViewRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(CGRectMake(0, 2, 40, 40), 10, 10) //Change frame according to your needs
    }
}

Step 2:Set text insets and left image from interface builder.

第 2 步:从界面构建器中设置文本插入和左图像。

enter image description here

在此处输入图片说明

Step 3:Enjoy.

第3步:享受。

回答by Paras Joshi

hey mate you want dropdown view then see this custom dropdown view ..

嘿,伙计,您想要下拉视图,然后查看此自定义下拉视图..

  1. http://code.google.com/p/dropdowndemo/downloads/list
  2. http://ameyashetti.wordpress.com/2010/09/26/drop-down-demo/
  1. http://code.google.com/p/dropdowndemo/downloads/list
  2. http://ameyashetti.wordpress.com/2010/09/26/drop-down-demo/

and for this requirement use this code

对于此要求,请使用此代码

UITextField *txtstate =[[UITextField alloc]init]; [txtstate setFrame:CGRectMake(10, 30,170, 30)]; 
txtstate.delegate=self; 

txtstate.text=@"Fruits"; 

txtstate.borderStyle = UITextBorderStyleLine; 

txtstate.background = [UIImage imageNamed:@"dropdownbtn.png"]; 

[txtstate setAutocorrectionType:UITextAutocorrectionTypeNo]; 
[self.view addSubview:txtstate];

and set your textfield border style none..

并设置你的文本框边框样式无..

回答by Andrey Chernukha

UITextField has the following property:

UITextField 具有以下属性:

@property(nonatomic, retain) UIImage *background

example:

例子:

UITextField *myTextField = [[UITextField alloc] init];
myTextField.background = [UIImage imageNamed:@"myImage.png"];

回答by Td Prashanth

      emailTextField.leftViewMode = .alway
    let emailImgContainer = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 25))
    let emailImg = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
    emailImg.image = UIImage(named: "SomeIMG")
    emailImgContainer.addSubview(emailImg)
    emailTextField.leftView = emailImgContainer

enter image description here

在此处输入图片说明