68

在此处输入图像描述

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

为此,我正在使用UIPickerview.

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

问题:

有没有办法,我可以添加图像TextField

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

4

9 回答 9

151

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

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

myTextField.rightViewMode = UITextFieldViewModeAlways;
myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]];
于 2012-11-22T09:15:22.377 回答
39

在斯威夫特:

textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = UIImageView(image: UIImage(named: "imageName"))
于 2014-11-21T22:28:14.303 回答
31

您可以将UIImageView放在UITextField.

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(96, 40, 130, 20)];
[textField setLeftViewMode:UITextFieldViewModeAlways];
textField.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIccon.png"]];
于 2013-07-11T17:06:15.470 回答
11

斯威夫特 3.0

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

带有下拉图标的文本字段

于 2016-10-06T11:18:06.347 回答
6

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

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

斯威夫特版本:

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

在此处输入图像描述

于 2016-06-30T15:01:32.930 回答
3

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

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

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

于 2012-11-22T08:59:39.487 回答
3

第 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 步:从界面生成器中设置文本插图和左侧图像。

在此处输入图像描述

第三步:享受。

于 2016-07-12T10:41:14.870 回答
2

UITextField 具有以下属性:

@property(nonatomic, retain) UIImage *background

例子:

UITextField *myTextField = [[UITextField alloc] init];
myTextField.background = [UIImage imageNamed:@"myImage.png"];
于 2012-11-22T08:55:32.973 回答
1
      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

在此处输入图像描述

于 2018-09-21T11:46:15.873 回答