我需要实现 dropdown
在Xcode中。
为此,我正在使用UIPickerview
。
当点击文本字段(在textFieldDidBeginEditing :)事件上时,将加载pickerView。
题:
有没有办法添加图像TextField
?
图像就像一个箭头标记,通过它用户可以了解点击时dropdown
出现。textfield
我该如何做呢?
我需要实现 dropdown
在Xcode中。
为此,我正在使用UIPickerview
。
当点击文本字段(在textFieldDidBeginEditing :)事件上时,将加载pickerView。
题:
有没有办法添加图像TextField
?
图像就像一个箭头标记,通过它用户可以了解点击时dropdown
出现。textfield
我该如何做呢?
Answers:
UITextField有一个rightView
属性,如果您有这样的图像,则可以轻松地将ImageView对象设置为rightView:
UITextField *myTextField = [[UITextField alloc] init];
myTextField.rightViewMode = UITextFieldViewModeAlways;
myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]];
在Swift中:
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = UIImageView(image: UIImage(named: "imageName"))
您可以将放在您UIImageView
的左侧UITextField
。
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(96, 40, 130, 20)];
[textField setLeftViewMode:UITextFieldViewModeAlways];
textField.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIccon.png"]];
要在图像和文本字段之间添加适当的边距/填充,请尝试以下代码。扩展@ 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
第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:从界面构建器设置文本插图和左侧图像。
第三步:享受。
嗨,您想要下拉菜单的队友,然后看到此自定义下拉视图..
并为此要求使用此代码
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];
并设置您的文本框边框样式为无。
UITextField具有以下属性:
@property(nonatomic, retain) UIImage *background
例:
UITextField *myTextField = [[UITextField alloc] init];
myTextField.background = [UIImage imageNamed:@"myImage.png"];
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