Add image to UITextField in Xcode?

enter image description here

I need to implement the dropdown concept in Xcode.

For this purpose I use UIPickerview .

This pickerView loads when a text field is listened (on the event textFieldDidBeginEditing :).

Question:

Is there a way to add an image to a TextField ?

The image is similar to an arrow mark, by which the user can understand that a dropdown appears when you click TextField . How can i do this?

+60
iphone uitextfield xcode uipickerview
Nov 22
source share
8 answers

UITextField has a rightView property, if you have this image - enter image description here then you can easily set the ImageView object to the right:

 UITextField *myTextField = [[UITextField alloc] init]; myTextField.rightViewMode = UITextFieldViewModeAlways; myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]]; 
+139
Nov 22 '12 at 9:15
source share

In Swift:

 textField.leftViewMode = UITextFieldViewMode.Always textField.leftView = UIImageView(image: UIImage(named: "imageName")) 
+32
Nov 21 '14 at 22:28
source share

You can put a UIImageView left of your UITextField .

 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(96, 40, 130, 20)]; [textField setLeftViewMode:UITextFieldViewModeAlways]; textField.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIccon.png"]]; 
+30
Jul 11 '13 at 17:06
source share

Swift 3.0

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

TextField with DropDown Icon

+6
Oct 06 '16 at 11:18
source share

Hey, you want to see a dropdown, and then look at this custom dropdown.

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 the border style of the text field to none ..

+3
Nov 22 '12 at 8:59
source share

To add the correct paddings between the image and the text box, try this code below. Answer extension @ King-Wizard.

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

Quick 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

+3
Jun 30 '16 at 15:01
source share

UITextField has the following property:

 @property(nonatomic, retain) UIImage *background 

Example:

 UITextField *myTextField = [[UITextField alloc] init]; myTextField.background = [UIImage imageNamed:@"myImage.png"]; 
+2
Nov 22 '12 at 8:55
source share

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

 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 the text inserts and the left image from the interface designer.

enter image description here

Step 3: Enjoy.

0
Jul 12 '16 at 10:41
source share



All Articles