Kristopher Johnson Kristopher Johnson - 6 months ago 30
iOS Question

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 the iPhone 3.0 and later SDKs, you can do this in Interface Builder.


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.