xcode 将按钮添加到 uitextfield
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14392804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add button to uitextfield
提问by dotnet-practitioner
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
当我在文本框内单击时,如何向 UITextField 添加加号按钮,如下面的屏幕所示?单击此按钮时,它将启动 iPhone 联系人应用程序。我非常感谢任何代码示例。谢谢
采纳答案by iPatel
Use following method for add UIButton
使用以下方法添加 UIButton
[self.txtField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (BOOL) textFieldDidChange:(UITextField *)textField
{
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:@selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(self.txtTag.bounds.size.width - 50, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:@"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtTag addSubview:btnColor];
return YES;
}
Or Write
或写
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// self.scrollView.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 250);
[UIView commitAnimations];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:@selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(150, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:@"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtField addSubview:btnColor];
return YES;
}
回答by Midhun MP
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.
如果您在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;
回答by Nimit Parekh
Following code use for adding button in UITextField.
以下代码用于在 UITextField 中添加按钮。
-(void)ViewDidLoad{
UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
txt.returnKeyType = UIReturnKeyDone;
txt.placeholder = @"Enter or Mobile Number";
[txt setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"crossImg.png"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:@selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
txt.rightView = button;
txt.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txt];
}
-(IBAction)clearText:(id)sender{
}
回答by Rahul Wakade
Use rightView
property of UITextField
.
In textFieldDidBeginEditing:
delegate create button with action you want & set to rightView
.
And in textFieldDidEndEditing:
set nil to rightView
使用 的rightView
属性UITextField
。在textFieldDidBeginEditing:
委托中创建带有您想要的操作的按钮并设置为rightView
. 并且在textFieldDidEndEditing:
设置 nil 到rightView
回答by Prajwal Udupa
you can also go for custom view ant then add sub view to it. then you hide it. it will be visible only when the user taps it
您还可以选择自定义视图蚂蚁,然后为其添加子视图。然后你隐藏它。只有当用户点击它时它才会可见
回答by Mannam Brahmam
self.locationName.clearButtonMode = UITextFieldViewModeWhileEditing;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 30, 30);
[button setImage:[UIImage imageNamed:@"goButtonIcon.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.locationName.rightView = button;
self.locationName.rightViewMode = UITextFieldViewModeUnlessEditing;
and then call this method:
然后调用这个方法:
-(IBAction)goButtonClicked:(id)sender{
}
self.locationName -------> UITextField Reference (in ViewDidload you can write this code)
self.locationName -------> UITextField 参考(在 ViewDidload 中您可以编写此代码)