dot*_*ner 15 iphone xcode objective-c ios
当我在文本框内单击时,如何向UITextField添加加号按钮,如下面的屏幕所示?单击此按钮时,将启动iPhone联系人应用程序.我非常感谢任何代码示例.谢谢
您可以使用以下代码:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果在IB中添加了字段,则可以使用上述方法.
如果您是通过代码创建的,则需要在创建时添加以下代码:
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;
Run Code Online (Sandbox Code Playgroud)
以下代码用于在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{
}
Run Code Online (Sandbox Code Playgroud)
使用以下方法进行添加 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;
}
Run Code Online (Sandbox Code Playgroud)
或写
-(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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13976 次 |
最近记录: |