Добавить кнопку в uitextfield

Когда я нажимаю внутри текстового поля, как я могу добавить кнопку плюс в UITextField, как показано на следующем экране? Когда эта кнопка будет нажата, она запустит приложение для контактов iPhone. Я был бы очень признателен за любой пример кода. Благодаря

enter image description here

Ответы

Ответ 1

Используйте следующий метод для добавления 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;
}

Или напишите

-(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;
}

Ответ 2

Следующий код используется для добавления кнопки в 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{

}

Ответ 3

Вы можете использовать приведенный ниже код для этого:

- (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;
  }
}

Этот метод можно использовать, если вы добавили поле в 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;

Ответ 4

Используйте rightView свойство UITextField. В textFieldDidBeginEditing: делегировать кнопку создания с нужным действием и установить rightView. И в textFieldDidEndEditing: установите nil на rightView

Ответ 5

вы также можете перейти к пользовательскому представлению ant, а затем добавить к нему под просмотр. то вы его спрячете. он будет виден только тогда, когда пользователь удалит его

Ответ 6

    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;

а затем вызовите этот метод:

-(IBAction)goButtonClicked:(id)sender{

}

self.locationName ------- > UITextField Reference (в ViewDidload вы можете написать этот код)