Swift add icon/image in UITextField

how to set image in uitextfield in ios
uitextfield left view image padding swift 4
uitextfield right view image padding swift
uitextfield leftview padding swift
add button to uitextfield swift
custom uitextfield swift
add image to uitextfield objective-c
uitextfield rightview button

I would like to add icon/image in UITextField. The icon/image should be left to placeholder.

I tried this:

var imageView = UIImageView();
var image = UIImage(named: "email.png");
imageView.image = image;
emailField.leftView = imageView;

Thanks.

Try adding emailField.leftViewMode = UITextFieldViewMode.Always

(Default leftViewMode is Never)

Updated Answer for Swift 4

emailField.leftViewMode = UITextFieldViewMode.always

emailField.leftViewMode = .always

Learn to create a stunning textfield with icon in Swift, Learn to create a stunning textfield with icon in Swift. Step 1: Add a basic UITextField and give it basic constraints: Step 2: Connect the UITextField to its ViewController's IBOutlet: Step 3: Add Icon to Assets.xcassets and set Render As to Template Image: Step 4: Extend UITextField and create a new setIcon function: Step 1: Add a basic UITextField and give it basic constraints: Center Horizontally & Center Vertically, also Constraint its Height and Width. Here I increased its height to 44px to make it

Add icon or image to UITextField in Swift 4 (Xcode 9.2), In this video, we are going to learn how to Add icon or image to UITextField in Swift 4. We are Duration: 9:10 Posted: Feb 12, 2018 if you want to add image on specific position you need to add ImageView as subview of UITextField. Update For Swift 3.0 @Mark Moeykens Beautifully expended it and make it @IBDesignable. I modified and added some more features (add Bottom Line and padding for right image) in this.

Sample code showing how to add icons to UITextFields · GitHub, Sample code showing how to add icons to UITextFields - UITextFiledIcon.swift. UITextFiledIcon.swift set frame on image before adding it to the uitextfield. In this video, we are going to learn how to Add icon or image to UITextField in Swift 4. We are going to create two UITextFields, one email textfield and other password textfield. Will give mail

Another way, inspired from previous posts to make an extension.

We can put the image on the right or on the left

extension UITextField {

enum Direction {
    case Left
    case Right
}

// add image to textfield
func withImage(direction: Direction, image: UIImage, colorSeparator: UIColor, colorBorder: UIColor){
    let mainView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 45))
    mainView.layer.cornerRadius = 5

    let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 45))
    view.backgroundColor = .white
    view.clipsToBounds = true
    view.layer.cornerRadius = 5
    view.layer.borderWidth = CGFloat(0.5)
    view.layer.borderColor = colorBorder.cgColor
    mainView.addSubview(view)

    let imageView = UIImageView(image: image)
    imageView.contentMode = .scaleAspectFit
    imageView.frame = CGRect(x: 12.0, y: 10.0, width: 24.0, height: 24.0)
    view.addSubview(imageView)

    let seperatorView = UIView()
    seperatorView.backgroundColor = colorSeparator
    mainView.addSubview(seperatorView)

    if(Direction.Left == direction){ // image left
        seperatorView.frame = CGRect(x: 45, y: 0, width: 5, height: 45)
        self.leftViewMode = .always
        self.leftView = mainView
    } else { // image right
        seperatorView.frame = CGRect(x: 0, y: 0, width: 5, height: 45)
        self.rightViewMode = .always
        self.rightView = mainView
    }

    self.layer.borderColor = colorBorder.cgColor
    self.layer.borderWidth = CGFloat(0.5)
    self.layer.cornerRadius = 5
}

}

Use :

if let myImage = UIImage(named: "my_image"){
    textfield.withImage(direction: .Left, image: myImage, colorSeparator: UIColor.orange, colorBorder: UIColor.black)
}

Enjoy :)

Swift add icon/image in UITextField, I would like to add icon/image in UITextField. @IBDesignable so developers can add images to their UITextFields on the Storyboard. Swift 3 https://www.bigmountainstudio.com Can you put images or icons in a UITextField? Watch and find out! NOTE: One thing people have asked for is padding between image and text. I've updated this class

for Swift 3.0 add image on leftside of textField

textField.leftView = UIImageView(image: "small-calendar")
textField.leftView?.frame = CGRect(x: 0, y: 5, width: 20 , height:20)
textField.leftViewMode = .always

Swift add icon/image in UITextField, I would like to add icon/image in UITextField. The icon/image should be left to placeholder. I tried this: var imageView = UIImageView(); var image  if your image background is white then image won't visible on UITextField. if you want to add an image to the specific position you need to add ImageView as the subview of UITextField. Update For Swift 3.0

How to place custom image in UITextField in iOS, Here is how to put an image in the right portion of a UITextField : UITextField *​textField = [[UITextField alloc] initWithFrame:CGRectMake(55, 10,  The background image to display when the text field is enabled. This image is displayed behind the rest of the text field’s content. To set this attribute programmatically, use the background property. Disabled. The background image to display when the text field is disabled. This image is displayed behind the rest of the text field’s content.

Swift add icon/image in UITextField, I would like to add icon/image in UITextField. The icon/image should be left to placeholder. enter image description here. I tried this: var imageView  In this Video , we Learn 1. how to set padding for uitextfield in ios 2. UITextField border for bottom side only in swift | Creating a textfield with only bottom Line in Ios.

leftView, Declaration. var leftView: UIView? { get set } The image associated with this property should fit the given rectangle. If it does var leftViewMode: UITextField. SearchTextField is a subclass of UITextField, written in Swift that makes really easy the ability to show an autocomplete suggestions list. You can decide wether to show the list as soon as the field is focused or when the user starts typing.

Comments
  • You accidentally added semicolons :D
  • emailField.leftView = UIImageView(image: UIImage(named: "search")) :)
  • Hmm it worked for me, did you try it with the updated value (UITextFieldViewMode.Always)? Do you have any idea of what it was that didn't work? (for instance did it compile?)
  • This is my whole code in ' ViewWillAppear' section: ' var imageView = UIImageView(); var image = UIImage(named: "email.png"); imageView.image = image; emailTextField.leftView = imageView; emailTextField.leftViewMode = UITextFieldViewMode.Always
  • @informatiker Ok, So are you sure that you have the connections correct, if you for example set the background color of the emailTextField does it really change? And are you sure that the image is there? For example, create an other UIImageView and load the image in that one to verify that that works.
  • yes, thanks. The issue was that the icon was white and wasn't visible on white background.
  • I continued my search and finally got it to work, i missed setting up the frame for the image view. Look's like we need to set frame on image before adding it to the uitextfield. Something like this imageView1.frame = CGRect(x: 5, y: 0, width: userName.frame.height, height: userName.frame.height) view.addSubview(imageView1)
  • Added in imageView.contentMode = .AspectFit, to avoid image resizing.
  • Thanks a lot for the great post! I've modified it to also have a RTL option. Code available here: pastebin.com/7CCm6NEB
  • Nice job, guys! I had ended up doing the same thing later on too. Ha ha
  • what about padding between image and text?
  • the Great answer.
  • how can i add this in extensions and call in my viewcontroller ? can you suggest me