1

I am using xcode8.3 and Swift3 in my project.

I have a UITextField in storyboard, I created a IBOutlet in corresponding controller class:

@IBOutlet weak var myTextField: UITextField!

I would like to show a small image on the right side inside the text field, I found a solution here (though it shows image on left side).

THis is what I tried:

override func viewDidLoad() {
        super.viewDidLoad()

        let imageView = UIImageView();
        let image = UIImage(named: "my_icon");
        imageView.image = image;

        self.myTextField.rightView = imageView
        self.myTextField.rightViewMode = .always
  }

(In xcode Assets.xcassets folder I have my_icon.png, my_icon@2x.png, my_icon@3x.png)

When I run the code on iPhone, I don't see my_icon showing anywhere in UITextField. I also tried to add the ".png" suffix when loading image let image = UIImage(named: "my_icon.png");, it doesn't help either.

Why the solution in the link doesn't work for me?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • here you have a bunch of information about https://stackoverflow.com/questions/27903500/swift-add-icon-image-in-uitextfield – GIJOW Aug 31 '17 at 14:49
  • @GIJOW, I have already included this link in my question, like I said, I tried it, it doesn't work for me. – Leem.fin Aug 31 '17 at 14:50
  • Clarify issues: is `image` nil? Also, You may want also to set a frame for `imageView`. – Larme Aug 31 '17 at 14:51

2 Answers2

2

I had similar issue, it sounds funny but it can cause the issue. I believe your imageView's frame is 0.

So

    let imageView = UIImageView();
    let image = UIImage(named: "my_icon");
    imageView.image = image;
    imageView.frame = CGRect(x: 0, y: 0, width: imageView.intrinsicContentSize.width, height: imageView.intrinsicContentSize.height)
    self.myTextField.rightView = imageView
    self.myTextField.rightViewMode = .always
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • Thanks, it works. BTW, if I want to hide the image later on, is it better to set the rightViewMode to Never or is it better to hide the image? – Leem.fin Aug 31 '17 at 14:55
  • @leem-fin : I personally prefer removing the rightView and then setting mode to never but setting the rightView's frame to CGRect.zero works as well. – Sandeep Bhandari Aug 31 '17 at 14:56
  • Thanks, one more question, how to add some space at the bottom and right of the image ? – Leem.fin Aug 31 '17 at 14:57
  • @leem-fin : Did you try setting ContentInset. Setting ContentInset.right and bottom should help. I never tried my self just guessing. – Sandeep Bhandari Aug 31 '17 at 14:59
  • I don't find function to set ContentInset for either UITextField nor UIImage, what do you mean? Could you be more specific please? – Leem.fin Aug 31 '17 at 15:01
  • @leem-fin : You have to create a subclass of textField and then override textRectForBounds I have answered it earlier in SO searching for it gimme a minute Ill post the link – Sandeep Bhandari Aug 31 '17 at 15:05
  • @leem-fin : Please have a look at https://stackoverflow.com/questions/25367502/create-space-at-the-beginning-of-a-uitextfield not mine though :P – Sandeep Bhandari Aug 31 '17 at 15:05
  • @leem-fin : Finally have a look at this https://stackoverflow.com/questions/18371053/position-of-rightview-uitextfield to add padding to rightView. Happy coding :) – Sandeep Bhandari Aug 31 '17 at 15:07
  • 1
    Thanks! I will take a close look into it. – Leem.fin Aug 31 '17 at 15:10
1

Set frame:

  imageView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
Aleksandr Honcharov
  • 2,343
  • 17
  • 30