1

I am showing a UITextField to the user and the text inside this text field is updated by a different controller.

User cannot type inside this UITextField. I ensured this by doing the following:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return NO;
}

But I want to show the clear icon in the text field, so that the user can clear the value in textfield. I do that by doing the following:

_inputField.clearButtonMode = UITextFieldViewModeAlways;

But the clear button does not show up when there is a text inside it :(

Any idea what I might be doing wrong?

Pranav Raj
  • 781
  • 1
  • 8
  • 19

3 Answers3

0

Try this:
customTextField.clearButtonMode = UITextFieldViewMode.Always

customTextField.clearsOnBeginEditing = true;

func textFieldShouldClear(textField: UITextField) -> Bool { return true }

Mr. SS
  • 387
  • 2
  • 19
0

You need to create a custom button and add it to textfield's rightView

UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[clearButton setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[clearButton setFrame:CGRectMake(0,0,45,45)];
// If you need action
[clearButton addTarget:self action:@selector(clearTextField) forControlEvents:UIControlEventTouchUpInside];


 _inputField.rightViewMode = UITextFieldViewModeAlways;
 _inputField.rightView = clearButton;
 _inputField.rightView.hidden = NO;
Clown
  • 163
  • 1
  • 12
0

I think you forgot to add UITextFieldDelegate and set delegate to your textField

_inputField.delegate = self;
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19