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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,552评论 5 6
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,949评论 0 23
  • 雨过天晴的午后,来到凤凰公园,湖面平静、碧波荡漾,凭阑处 潇潇雨歇。 湖中风景如画,引得花儿飞去飞去,...
    晚起画蛾眉阅读 300评论 0 2
  • 我曾经到过一个城市六年却未曾好好看过它的风景,而当我即将离开它的时候,又埋怨它没有给予我欢乐,相反,是一夜夜...
    长世阅读 369评论 0 0