ios 在 UITextField 中输入一个 rightview UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19240775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enter a rightview UIImageView to a UITextField
提问by cdub
I have a target action that when a button is pressed, I validate the UITextField
:
我有一个目标操作,当按下按钮时,我验证UITextField
:
// Get the text from the UITextField
NSString *nameStr = _name.text;
_name.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
[self.view addSubview:_name];
if (nameStr.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Name"
message:@"You must enter a name."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
My UITextField
are added to the view in viewDidLoad
like so:
我UITextField
被添加到视图中,viewDidLoad
如下所示:
[self.view addSubview:_name];
How do I make a rightView UIImageView
appear?
如何使 rightViewUIImageView
出现?
回答by Toseef Khilji
You have to set rightViewMode
of the Textfield because The default value for this property is UITextFieldViewModeNever.
您必须设置rightViewMode
Textfield,因为此属性的默认值是UITextFieldViewModeNever。
so You have to set Mode from following,
所以你必须从以下设置模式,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
回答by iNoob
Are you sure you are not missing the UITextField
frame ? And are you setting the rightViewMode
?
你确定你没有错过UITextField
框架吗?你设置了rightViewMode
吗?
This is an example i just wrote seem to be working fine.
这是我刚刚写的一个例子,似乎工作正常。
UITextField *rightField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 140, 50)];
rightField.backgroundColor = [UIColor redColor]; // For testing purpose
rightField.rightViewMode = UITextFieldViewModeAlways;// Set rightview mode
UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tick_white_color"]];
rightField.rightView = rightImageView; // Set right view as image view
[self.view addSubview:rightField];
Here is the tick image as result :
这是结果的刻度图像:
回答by Maor
Solution in Swift 3:
Swift 3 中的解决方案:
class CustomText: UITextField {
@IBInspectable var rightImage : UIImage?{
didSet{
self.rightView = UIImageView(image: rightImage)
// select mode -> .never .whileEditing .unlessEditing .always
self.rightViewMode = .always
}
}
}
回答by Hardik Thakkar
For Swift 3.To show image before placeholder text in UITextField
对于 Swift 3.To 在占位符文本之前显示图像 UITextField
func setPaddingView(strImgname: String,txtField: UITextField){
let imageView = UIImageView(image: UIImage(named: strImgname))
imageView.frame = CGRect(x: 0, y: 5, width: imageView.image!.size.width , height: imageView.image!.size.height)
let paddingView: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
paddingView.addSubview(imageView)
txtField.leftViewMode = .always
txtField.leftView = paddingView
}
To call Function
调用函数
self.setPaddingView(strImgname: "mail", txtField: txtEmail)
回答by jayesh lathiya
If you have two or more fieldthen try this code! its working for me.
如果您有两个或更多字段,请尝试使用此代码!它为我工作。
dropDownImageViewForTextField1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
dropDownImageViewForTextField1.image = [UIImage imageNamed:@"dropdown_n_icon.png"];
dropDownImageViewForTextField2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
dropDownImageViewForTextField2.image = [UIImage imageNamed:@"dropdown_n_icon.png"];
txtField1.rightViewMode = UITextFieldViewModeAlways;
txtField1.rightView = dropDownImageViewForTextField1;
txtField2.rightViewMode = UITextFieldViewModeAlways;
txtField2.rightView = dropDownImageViewForTextField2;
Do check 2 things
做检查2件事
txtField.rightViewMode = UITextFieldViewModeAlways
- Separate
imageView
for eachtextField
txtField.rightViewMode = UITextFieldViewModeAlways
- 分别
imageView
为每个textField
回答by elia
Solutionwith @IBDesignable
and Swift 3
解决方案与@IBDesignable
和斯威夫特3
Firstly, you can create @IBDesignable
for UITextField
like this.
首先,你可以创建@IBDesignable
的UITextField
这个样子。
@IBDesignable extension UITextField{
@IBInspectable var setImageName: String {
get{
return ""
}
set{
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: newValue)!
self.rightView = imageView
self.rightViewMode = UITextFieldViewMode.always
}
}
Note: You must be write getand setpoints for @IBInspactable
block because Swifthas warn to apply getter and settertogether. We've not need to use the get statement, so returned empty string. Also the attribute name has include Button but not confuse, its a mistake.
注意:您必须为块编写get和set点,@IBInspactable
因为Swift警告将 getter 和setter一起应用。我们不需要使用 get 语句,因此返回空字符串。此外,属性名称包含 Button 但不要混淆,这是一个错误。
Then click the UITextField
on StoryBoard
and give the spesific image name to the setAButtonImageNameattribute.
然后单击UITextField
onStoryBoard
并将特定图像名称赋予setAButtonImageName属性。
Then you will get the following result
然后你会得到以下结果
The other situation is the create rightView with seems like has right margin
另一种情况是创建 rightView 似乎有右边距
Here is the screenshot.
这是屏幕截图。
You can use a UIView
as containerthan can add the UIImageView
into the this UIView
. In here, you must calculate the right margin, container view's widthand `UIImageViews' width.
您可以使用 aUIView
作为容器而不是将 加入UIImageView
到 this 中UIView
。在这里,您必须计算右边距、容器视图的宽度和`UIImageViews' 的宽度。
@IBInspectable var setImageName: String {
get{
return ""
}
set{
let containerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width:50, height: self.frame.height))
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: newValue)!
containerView.addSubview(imageView)
imageView.center = containerView.center
self.rightView = containerView
self.rightViewMode = UITextFieldViewMode.always
}
}