UITextField 右边 放 Button

add button to uitextfield



When I click inside text box, How can I add plus button to UITextField as shown in the following screen? When this button is clicked it would launch iPhone contact application. I would very much appreciate any code example. Thanks



You can use the below code for this:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 

if(textField == yourFirstTextField)
 { 
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [addButton setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal]; 
[addButton addTarget:self action:@selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside]; 

textField.rightViewMode = UITextFieldViewModeWhileEditing; 
textField.rightView = addButton;
 }
}

You can use the above method if you added the field in IB.
If you are created it through code, you need to add the below code when creating:

UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];

[addButton setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:@selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];

textField.rightViewMode = UITextFieldViewModeWhileEditing;

textField.rightView = addButton;

你可能感兴趣的:(UITextField 右边 放 Button)