68

enter image description here

I need to implement the concept of dropdown in Xcode.

For that purpose, I'm using a UIPickerview.

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

Question:

Is there a way, that I can add a image to 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?

Irfan
  • 4,301
  • 6
  • 29
  • 46
Honey
  • 2,840
  • 11
  • 37
  • 71

9 Answers9

151

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

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

myTextField.rightViewMode = UITextFieldViewModeAlways;
myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]];
Zorayr
  • 23,770
  • 8
  • 136
  • 129
Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
39

In Swift:

textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = UIImageView(image: UIImage(named: "imageName"))
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
  • elegant solution !, but i also have some trouble to set a background image for my uitexfield (not left view, but the whole background), can you tell me how ?, in swift. – Bhimbim Apr 14 '15 at 08:36
  • to set correct margins / padding on image, first create a view with little bit bigger dimension than image. And create the image view, center it in the UIView, add UIImageView to UIView. Now final step is to set this UIView as .leftView. – kishorer747 Jun 30 '16 at 14:54
31

You can put the UIImageView to the 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"]];
Anil
  • 2,430
  • 3
  • 37
  • 55
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44
11

Swift 3.0

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

TextField With DropDown Icon

Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
6

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

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

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

kishorer747
  • 810
  • 1
  • 10
  • 24
3

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/

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..

Mohamed Elkassas
  • 829
  • 1
  • 13
  • 33
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • txtstate =[[UITextField alloc]init]; [txtstate setFrame:CGRectMake(10, 30,170, 30)]; txtstate.delegate=self; txtstate.text=@"Fruits"; txtstate.borderStyle = UITextBorderStyleNone; txtstate.background = [UIImage imageNamed:@"Arrow-Down-black-32.png"]; [txtstate setAutocorrectionType:UITextAutocorrectionTypeNo]; [self.view addSubview:txtstate]; Did like this but I need borders also..How can I do it? – Honey Nov 22 '12 at 10:35
  • which you use mate?? means you use this custom dropdown?? – Paras Joshi Nov 22 '12 at 10:38
  • @arizah you did checkout the demo of dropdown.. i think this is very useful dude.. and also i give the border with many type if you want to border then i post code just tell me you required the code now?? – Paras Joshi Nov 22 '12 at 10:50
  • Yes I went through it ...I got it..Thanks – Honey Nov 22 '12 at 10:51
  • k mate Thanks a lot for ur words..I do have some doubts in other concepts..Can I get ur mail Id? – Honey Nov 22 '12 at 11:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19977/discussion-between-arizah-and-paras-joshi) – Honey Nov 23 '12 at 06:50
3

Step 1: Add this class to your project and add it to your textFiled's class in 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 text insets and left image from interface builder.

enter image description here

Step 3: Enjoy.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
2

UITextField has the following property:

@property(nonatomic, retain) UIImage *background

example:

UITextField *myTextField = [[UITextField alloc] init];
myTextField.background = [UIImage imageNamed:@"myImage.png"];
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
2
      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