将图像添加到 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
Add image to UITextField in Xcode?
提问by Honey
I need to implement the concept of dropdown
in 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 dropdown
appears when textfield
is tapped .How can I make it?
图像就像一个箭头标记,用户可以通过它来理解点击dropdown
时textfield
会出现a。我该如何制作?
回答by Mayur Birari
UITextField has a rightView
property, if you have image like this- then 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 UIImageView
to 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
回答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
回答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 步:从界面构建器中设置文本插入和左图像。
Step 3:Enjoy.
第3步:享受。
回答by Paras Joshi
hey mate you want dropdown view then see this custom dropdown view ..
嘿,伙计,您想要下拉视图,然后查看此自定义下拉视图..
- http://code.google.com/p/dropdowndemo/downloads/list
- http://ameyashetti.wordpress.com/2010/09/26/drop-down-demo/
- http://code.google.com/p/dropdowndemo/downloads/list
- 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