简体   繁体   English

Swift UITextField图标位置

[英]Swift UITextField icon position

I'm trying to set the position of my custom UITextField icon. 我正在尝试设置自定义UITextField图标的位置。 But when I change the imageView's frame's x and y values, it still places them top left. 但是当我更改imageView的框架的x和y值时,它仍然将它们放在左上角。

Here's the viewWillAppear method of my custom UITextField class: 这是我的自定义UITextField类的viewWillAppear方法:

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. 更改CGRectMake调用中的x和y参数不会执行任何操作。

Any ideas? 有任何想法吗? I've read that this may be caused by the element's AutoLayout feature. 我读过这可能是由元素的AutoLayout功能引起的。 But if I remove that in the StoryBoard then XCode warns me about the app not looking great on multiple devices... 但如果我在StoryBoard中删除它,那么XCode警告我应用程序在多个设备上看起来不太好......

Actually you are setting UIImageView to leftView of UITextField . 实际上你正在将UIImageView设置为UITextField leftView so it alway comes in left side. 所以它总是在左侧。 if you want UIImageView to right side then there is rightView option. 如果你想让UIImageView在右边,那么就有rightView选项。 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 . 如果你想在你的特定位置添加UIImageView,我建议你add as subview而不是将其分配给leftViewrightView

Hope this help you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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