Adding the "Clear" Button to an iPhone UITextField

How do you add that little "X" button on the right side of a UITextField that clears the text? I can't find an attribute for adding this sub-control in Interface Builder in the iPhone OS 2.2 SDK.

Note: In Xcode 4.x and later (iPhone 3.0 SDK and later), you can do this in Interface Builder.

Answers


This button is a built-in overlay that is provided by the UITextField class, but as of the iPhone OS 2.2 SDK, there isn't any way to set it via Interface Builder. You have to enable it programmatically.

Add this line of code somewhere (viewDidLoad, for example):

myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

UPDATE: The latest version of Xcode IB does support this property.


You can also set this directly from Interface Builder under the Attributes Inspector.

Taken from XCode 5.1


Swift 3:

textField.clearButtonMode = UITextFieldViewMode.whileEditing

or even shorter:

textField.clearButtonMode = .whileEditing

you can add custom clear button and control the size and every thing using this:

UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[clearButton setImage:img forState:UIControlStateNormal];
[clearButton setFrame:frame];
[clearButton addTarget:self action:@selector(clearTextField:) forControlEvents:UIControlEventTouchUpInside];

textField.rightViewMode = UITextFieldViewModeAlways; //can be changed to UITextFieldViewModeNever,    UITextFieldViewModeWhileEditing,   UITextFieldViewModeUnlessEditing
[textField setRightView:clearButton];

Objective C :

self.txtUserNameTextfield.myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

Swift :

txtUserNameTextfield.clearButtonMode = UITextFieldViewMode.WhileEditing;

this don't work, do like me:

swift:

customTextField.clearButtonMode = UITextFieldViewMode.Always

customTextField.clearsOnBeginEditing = true;

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

Swift 4 (adapted from Kristopher Johnson's answer)

textfield.clearButtonMode = .always

textfield.clearButtonMode = .whileEditing

textfield.clearButtonMode = .unlessEditing

textfield.clearButtonMode = .never

On Xcode 8 (8A218a):

Swift:

textField.clearButtonMode = UITextFieldViewMode.whileEditing;

The "W" went from capital to non-cap "w".


  func clear_btn(box_is : UITextField){
    box_is.clearButtonMode = .always
    if let clearButton = box_is.value(forKey: "_clearButton") as? UIButton {
        let templateImage =  clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate)

        clearButton.setImage(templateImage, for: .normal)
        clearButton.setImage(templateImage, for: .highlighted)

        clearButton.tintColor = .white

     }
}

On Xcode Version 8.1 (8B62) it can be done directly in Attributes Inspector. Select the textField and then choose the appropriate option from Clear Button drop down box, which is located in Attributes Inspector.


Need Your Help

Not able to update UI in windows form

winforms user-interface thread-safety

I have one windows form in which I have a number of images shown using delegate. When one event is fired I need to remove image and put label saying image not shown. I have created new thread to up...

Changing the background color of the unified NSToolbar (in Yosemite)

cocoa swift

I want to have a unified toolbar and therefore I found this post: