Hide the cursor of a UITextField

Viewed 72460

I am using a UITextField with a UIPickerView for its inputView, so that when the user taps the text field, a picker is summoned for them to select an option from.

Nearly everything works, but I have one problem: the cursor still flashes in the text field when it is active, which is ugly and inappropriate, since the user is not expected to type into the field and is not presented with a keyboard. I know I could hackily solve this by setting editing to NO on the text field and tracking touches on it, or by replacing it with a custom-styled button, and summoning the picker via code. However, I want to use the UITextFieldDelegate methods for all the event handling on the text field and hacks such as replacing the text field with a button do not permit this approach.

How can I simply hide the cursor on the UITextField instead?

15 Answers

As of iOS 7 you can now just set the tintColor = [UIColor clearColor] on the textField and the caret will disappear.

set the tintColor to Clear Color

textfield.tintColor = [UIColor clearColor];

and you can also set from the interface builder

If you like cleaner = less code, use the interface builder:

Clear color

(Attributes inspector, view section.)

In my case, overriding the caret rect wasn't enough. On iOS 15, the caret didn't appear, effectively, but the selection handles did.

Solved it with: override var canBecomeFirstResponder: Bool { return false } on the UITextView subclass.

Related