ios – 向UITextField输入右视图UIImageView

前端之家收集整理的这篇文章主要介绍了ios – 向UITextField输入右视图UIImageView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个目标操作,当按下按钮时,我验证UITextFields:
  1. // Get the text from the UITextField
  2. NSString *nameStr = _name.text;
  3.  
  4. _name.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
  5. [self.view addSubview:_name];
  6.  
  7. if (nameStr.length == 0)
  8. {
  9. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Name"
  10. message:@"You must enter a name."
  11. delegate:nil
  12. cancelButtonTitle:@"OK"
  13. otherButtonTitles:nil];
  14. [alert show];
  15. }

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

  1. [self.view addSubview:_name];

如何使右视图UIImageView出现?

解决方法

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

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

  1. UITextField *rightField = [[UITextField alloc] initWithFrame:CGRectMake(0,140,50)];
  2. rightField.backgroundColor = [UIColor redColor]; // For testing purpose
  3. rightField.rightviewmode = UITextFieldviewmodeAlways;// Set rightview mode
  4.  
  5. UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tick_white_color"]];
  6. rightField.rightView = rightImageView; // Set right view as image view
  7.  
  8. [self.view addSubview:rightField];

结果如下:

猜你在找的iOS相关文章