Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display this little white x-circle in a UITextField, so users can delete the text?

Some apps have this nice little white circle with an gray x in there UITextField. How can I add this to an UITextField and delete the text when it is tapped?

like image 272
openfrog Avatar asked Sep 25 '10 09:09

openfrog


1 Answers

To add a button like this, you can use the clearButtonMode property of an UITextField.
The code will be like this:

// Show cancel button never
textField.clearButtonMode = UITextFieldViewModeNever;  

// Show cancel button only when you're editing text in textField
textField.clearButtonMode = UITextFieldViewModeWhileEditing;  

// Show the cancel button only when you aren't editing the text
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;

// Show always the cancel button
textField.clearButtonMode = UITextFieldViewModeAlways;
like image 117
matteodv Avatar answered Nov 02 '22 22:11

matteodv