13

我有一个目标操作,当按下按钮时,我验证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];
}

我像这样UITextField添加到视图中viewDidLoad

[self.view addSubview:_name];

如何使 rightViewUIImageView出现?

4

6 回答 6

30

您必须设置rightViewModeTextfield,因为此属性的默认值为UITextFieldViewModeNever

所以你必须从以下设置模式,

   UITextFieldViewModeWhileEditing,
   UITextFieldViewModeUnlessEditing,
   UITextFieldViewModeAlways
于 2013-10-08T07:03:47.180 回答
26

你确定你没有错过UITextField框架吗?你在设置rightViewMode吗?

这是我刚刚写的一个例子似乎工作正常。

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];

这是作为结果的刻度图像:

右图像视图

于 2013-10-08T06:59:07.493 回答
7

对于 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
    }

调用函数

self.setPaddingView(strImgname: "mail", txtField: txtEmail)

在此处输入图像描述

于 2017-04-06T11:14:41.110 回答
6

Swift 3 中的解决方案:

class CustomText: UITextField {
    @IBInspectable var rightImage : UIImage?{
        didSet{
            self.rightView = UIImageView(image: rightImage)
            // select mode -> .never .whileEditing .unlessEditing .always
            self.rightViewMode = .always
        }
    }
}
于 2017-03-27T07:54:35.833 回答
0

如果您有两个或更多字段,请尝试此代码!它为我工作。

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;

检查两件事

  1. txtField.rightViewMode = UITextFieldViewModeAlways
  2. 分别imageView为每个textField
于 2017-06-20T12:20:11.777 回答
0

使用Swift 3@IBDesignable解决方案

首先,您可以像这样创建@IBDesignableUITextField

@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
        }

    }

注意:您必须为block编写getset点,因为Swift已警告将 getter 和setter一起应用。我们不需要使用 get 语句,所以返回空字符串。属性名称也包含按钮但不要混淆,这是一个错误。@IBInspactable

然后单击UITextFieldonStoryBoard并将特定的图像名称赋予setAButtonImageName属性。

在此处输入图像描述

然后你会得到以下结果

在此处输入图像描述

另一种情况是创建 rightView 似乎有右边距

这是屏幕截图。

在此处输入图像描述

您可以使用 aUIView作为容器,而不是将其添加UIImageView到 thisUIView中。在这里,您必须计算右边距、容器视图的宽度和 `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
        }
    }
于 2018-02-09T07:25:42.357 回答