ios – 向UITextField输入右视图UIImageView

前端之家收集整理的这篇文章主要介绍了ios – 向UITextField输入右视图UIImageView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个目标操作,当按下按钮时,我验证UITextFields:
// 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];
}

我的UITextFields添加到viewDidLoad中的视图,如下所示:

[self.view addSubview:_name];

如何使右视图UIImageView出现?

解决方法

你确定你没有丢失UITextField框架吗?你正在设置rightviewmode吗?

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

UITextField *rightField = [[UITextField alloc] initWithFrame:CGRectMake(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];

结果如下:

猜你在找的iOS相关文章