UITextField clearButtonMode Always doesn't work - any workaround?

Old unanswered thread on the old forums: UITextView Cannot Set Clear Button to Always


I am creating a UITextField in code and setting its clearButtonMode to UITextFieldViewModeAlways. This has no effect (well it's the same as UITextFieldViewModeWhileEditing). The clear button does not appear until the text is non-empty. Same happens if you just drag a UITextField out in IB and set the clear button mode to Always.


I've submitted a bug (21602318) but I'm wondering if there's any workaround. I'm trying to "minimalize" a portion of UI and want the clear button to double as the cancel button. (Tap on search field to expand it and shows the X... tap the X to clear and shrink the search field.) I haven't tried using a custom button in the rightView since I would prefer to use the system-defined clear button.

Accepted Reply

AFAIK UITextFieldViewModeAlways just mean to show the clear button even when the text field is not first responder, while UITextFieldViewModeWhileEditing shows the button when editing only.

BUT both modes don't show the button if there's nothing to clear at all. And I don't think it is possible to have a UITextField to show a clear button if it has no content.


Dirk

Replies

AFAIK UITextFieldViewModeAlways just mean to show the clear button even when the text field is not first responder, while UITextFieldViewModeWhileEditing shows the button when editing only.

BUT both modes don't show the button if there's nothing to clear at all. And I don't think it is possible to have a UITextField to show a clear button if it has no content.


Dirk

I guess that makes sense if you squint your eyes and hold your head just right. To me "always" means "always".


I will update my bug report with a request that the documentation be clarified, if that is the way it works.