【问题标题】:Add image to UITextField in Xcode?在 Xcode 中将图像添加到 UITextField?
【发布时间】:2012-11-22 08:51:09
【问题描述】:

我需要在 Xcode 中实现dropdown 的概念。

为此,我使用UIPickerview

当点击文本字段时(在 textFieldDidBeginEditing:) 事件上加载此 pickerView。

问题:

有没有办法可以将图像添加到TextField

图像就像一个箭头标记,用户可以通过它理解点击textfield 时出现dropdown。我该怎么做?

【问题讨论】:

  • 您可以在文本字段旁边添加一个按钮(带有箭头图像)以打开选择器视图。
  • 我可以以编程方式将 imageview 之类的内容添加到文本字段吗?
  • 如果您不希望用户编辑文本字段,您可以使用按钮代替文本字段。
  • 在下面查看 Andrey Chernukha 的回答

标签: iphone xcode uitextfield uipickerview


【解决方案1】:

UITextField 有一个rightView 属性,如果你有这样的图像- 那么你可以很容易地将 ImageView 对象设置为 rightView:

UITextField *myTextField = [[UITextField alloc] init];

myTextField.rightViewMode = UITextFieldViewModeAlways;
myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]];

【讨论】:

【解决方案2】:

在 Swift 中:

textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = UIImageView(image: UIImage(named: "imageName"))

【讨论】:

  • 优雅的解决方案!,但我也很难为我的 uitexfield 设置背景图像(不是左视图,而是整个背景),你能告诉我怎么做吗?,很快。
  • 要在图像上设置正确的边距/填充,首先创建一个尺寸比图像大一点的视图。并创建图像视图,在 UIView 中居中,将 UIImageView 添加到 UIView。现在最后一步是将此 UIView 设置为 .leftView。
【解决方案3】:

您可以将UIImageView 放在UITextField 的左侧。

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(96, 40, 130, 20)];
[textField setLeftViewMode:UITextFieldViewModeAlways];
textField.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIccon.png"]];

【讨论】:

  • 谢谢 Erhan,IMO 你的解决方案是最好和最短的!!!这对我帮助很大:)
  • 欢迎您戴维特:)。真的,我很欣赏你的好声音:)
【解决方案4】:

Swift 3.0

txtField.rightViewMode = .always
txtField.rightView = UIImageView(image: UIImage(named: "selectdrop"))

【讨论】:

    【解决方案5】:

    要在图像和文本字段之间添加适当的边距/填充,请尝试以下代码。 扩展@King-Wizard 的答案。

    这里我的 TextField 的高度是 44 检查附件图片以供参考。

    Swift 版本:

    emailTF.leftViewMode = .Always
    let emailImgContainer = UIView(frame: CGRectMake(emailTF.frame.origin.x, emailTF.frame.origin.y, 40.0, 30.0))
    let emailImView = UIImageView(frame: CGRectMake(0, 0, 25.0, 25.0))
    emailImView.image = UIImage(named: "image1")
    emailImView.center = emailImgContainer.center
    emailImgContainer.addSubview(emailImView)
    emailTF.leftView = emailImgContainer
    

    【讨论】:

      【解决方案6】:

      第 1 步:将此类添加到您的项目中,并将其添加到身份检查器中的 textFiled 类中,

      class MyCustomTextField: UITextField {
      
          @IBInspectable var inset: CGFloat = 0
      
          @IBInspectable var leftImage: String = String(){
              didSet{
                  leftViewMode = UITextFieldViewMode.Always
                  leftView = UIImageView(image: UIImage(named: leftImage))
              }
          }
      
          override func textRectForBounds(bounds: CGRect) -> CGRect {
              return CGRectInset(bounds, inset, inset)
          }
      
          override func editingRectForBounds(bounds: CGRect) -> CGRect {
              return textRectForBounds(bounds)
          }
      
          override func leftViewRectForBounds(bounds: CGRect) -> CGRect {
              return CGRectInset(CGRectMake(0, 2, 40, 40), 10, 10) //Change frame according to your needs
          }
      }
      

      第 2 步:从界面生成器中设置文本插入和左图。

      第 3 步:享受。

      【讨论】:

      • 您可以制作 UIImage 类型的 leftImage,这将为您提供一个不错的直接图像输入。
      【解决方案7】:

      嘿,伙计,您想要下拉视图,然后查看此自定义下拉视图..

      1. http://code.google.com/p/dropdowndemo/downloads/list
      2. http://ameyashetti.wordpress.com/2010/09/26/drop-down-demo/

      对于此要求,请使用此代码

      UITextField *txtstate =[[UITextField alloc]init]; [txtstate setFrame:CGRectMake(10, 30,170, 30)]; 
      txtstate.delegate=self; 
      
      txtstate.text=@"Fruits"; 
      
      txtstate.borderStyle = UITextBorderStyleLine; 
      
      txtstate.background = [UIImage imageNamed:@"dropdownbtn.png"]; 
      
      [txtstate setAutocorrectionType:UITextAutocorrectionTypeNo]; 
      [self.view addSubview:txtstate];
      

      并将您的文本字段边框样式设置为无..

      【讨论】:

      • txtstate =[[UITextField alloc]init]; [txtstate setFrame:CGRectMake(10, 30,170, 30)]; txtstate.delegate=自己; txtstate.text=@"水果"; txtstate.borderStyle = UITextBorderStyleNone; txtstate.background = [UIImage imageNamed:@"Arrow-Down-black-32.png"]; [txtstate setAutocorrectionType:UITextAutocorrectionTypeNo]; [self.view addSubview:txtstate];喜欢这样,但我也需要边框..我该怎么做?
      • 你用哪个mate??意思是你使用这个自定义下拉菜单??
      • @arizah 你确实检查了下拉菜单的演示。我认为这是非常有用的家伙。如果你想加边框,我也给了很多类型的边框,然后我发布代码告诉我你现在需要代码??
      • 是的,我通过了...我明白了..谢谢
      • k mate 非常感谢你的话..我对其他概念有一些疑问..我可以得到你的邮件 ID 吗?
      【解决方案8】:

      UITextField 具有以下属性:

      @property(nonatomic, retain) UIImage *background
      

      示例:

      UITextField *myTextField = [[UITextField alloc] init];
      myTextField.background = [UIImage imageNamed:@"myImage.png"];
      

      【讨论】:

      • 是的,但我应该如何将此 uiimage 链接到文本字段?
      【解决方案9】:
            emailTextField.leftViewMode = .alway
          let emailImgContainer = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 25))
          let emailImg = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
          emailImg.image = UIImage(named: "SomeIMG")
          emailImgContainer.addSubview(emailImg)
          emailTextField.leftView = emailImgContainer
      

      enter image description here

      【讨论】:

      • 嗨,欢迎来到 SO :)。你能在你的答案中添加更多细节吗?这将使您更容易理解您的解决方案。谢谢!
      • 点击那个“在此输入图片描述”就可以理解了
      猜你喜欢
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多