3

I'm trying to set the position of my custom UITextField icon. But when I change the imageView's frame's x and y values, it still places them top left.

Here's the viewWillAppear method of my custom UITextField class:

var imageView = UIImageView();
var image = UIImage(named: "user-icon.png");
imageView.image = image;
imageView.frame = CGRectMake(100, 0, 20, 19);
usernameTextField.leftView = imageView;
usernameTextField.leftViewMode = UITextFieldViewMode.Always

Changing the x and y parameters in the CGRectMake call does nothing.

Any ideas? I've read that this may be caused by the element's AutoLayout feature. But if I remove that in the StoryBoard then XCode warns me about the app not looking great on multiple devices...

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Sean H
  • 1,045
  • 1
  • 14
  • 32
  • The image associated with this property should fit the given rectangle. If it does not fit, it is scaled to fit. If you specify a control for your view, the control tracks and sends actions as usual. **as per apple doc for UITextField** https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITextField_Class/index.html#//apple_ref/occ/instp/UITextField/leftView – Saurabh Prajapati Apr 23 '15 at 12:50

1 Answers1

2

Actually you are setting UIImageView to leftView of UITextField. so it alway comes in left side. if you want UIImageView to right side then there is rightView option. if you want to add UIImageView to at your specific position then i suggest you to add as subview rather then assigning its to leftView or rightView.

Hope this help you.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43