简体   繁体   中英

UITextField leftView

是否可以使用leftView设置UITextField,这样如果用户点击UITextField,键盘会显示,但如果他们点击leftView中的图标,则会调用另一个方法(即显示UIPickerview的方法)?

你有没有检查过leftFieldViewMode是否设置为never

[textField setLeftViewMode:UITextFieldViewModeAlways];

Instantiate a button as u do normaly

 UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

 [btn setFrame:CGRectMake(0,0,30,30)];

 [btn setTitle:@"title" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];

Instantiate a textfield as you do normally

 UITextField *txtfield1=[[UITextField alloc]initWithFrame:CGRectMake(10, 10,300,30)];

[txtfield1 setBackgroundColor:[UIColor grayColor]];

[txtfield1 setPlaceholder:@" Enter"];

To set a view in left side(there is right view also available)

[txtfield1 setLeftView:btn];

if u want the view to show always then below code does it

[txtfield1 setLeftViewMode:UITextFieldViewModeAlways];

if u want the view to show never then

[txtfield1 setLeftViewMode:UITextFieldViewModeNever];

if u want the view to show if the textfield is not edited then below

[txtfield1 setLeftViewMode:UITextFieldViewModeUnlessEditing];

if u want the view to show if the textfield is edited then

[txtfield1 setLeftViewMode:UITextFieldViewModeWhileEditing];

button click event and pop over

-(void)BtnClick
{
  //Usual pop over coding goes here
}

Have you tried using a UIButton for this task? From the documentation for the leftView property:

If your overlay view does not overlap any other sibling views, it receives touch events like any other view. If you specify a control for your view, the control tracks and sends actions as usual. If an overlay view overlaps the clear button, however, the clear button always takes precedence in receiving events.

So create a UIButton instance, configure its appearance and actions as needed, then set it as the leftView property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM