简体   繁体   中英

Swift UITextField icon position

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM