我需要dropdown
在 Xcode 中实现这个概念。
为此,我正在使用UIPickerview
.
当点击文本字段时(在 textFieldDidBeginEditing:) 事件上加载此 pickerView。
问题:
有没有办法,我可以添加图像TextField
?
图像就像一个箭头标记,用户可以通过它理解点击dropdown
时会出现a。textfield
我该怎么做?
我需要dropdown
在 Xcode 中实现这个概念。
为此,我正在使用UIPickerview
.
当点击文本字段时(在 textFieldDidBeginEditing:) 事件上加载此 pickerView。
问题:
有没有办法,我可以添加图像TextField
?
图像就像一个箭头标记,用户可以通过它理解点击dropdown
时会出现a。textfield
我该怎么做?
UITextField 有一个rightView
属性,如果你有这样的图像,那么你可以很容易地将 ImageView 对象设置为 rightView:
UITextField *myTextField = [[UITextField alloc] init];
myTextField.rightViewMode = UITextFieldViewModeAlways;
myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]];
在斯威夫特:
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
嘿,伙计,您想要下拉视图,然后查看此自定义下拉视图..
并为此要求使用此代码
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];
并将您的文本字段边框样式设置为无..
第 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 具有以下属性:
@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