Hide the cursor of a UITextField
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The insertion cursor in a UITextField is called the caret, and iOS normally shows it whenever the field becomes first responder. If you want the text field to stay editable but visually hide the caret, the most reliable solution is to subclass UITextField and override caretRect(for:) to return .zero.
Hide The Caret By Subclassing UITextField
This is the standard approach:
Use it like any other text field:
The field still accepts input, but the blinking insertion caret is not drawn.
This is usually the cleanest answer when the goal is specifically "hide the cursor" without disabling editing.
A Simpler Visual Trick: Clear The Tint Color
Another common trick is setting the text field's tintColor to clear:
This can hide the caret too, because the caret uses the control's tint color.
However, it is a broader visual change than overriding caretRect(for:). Depending on the situation, it may also affect selection handles or other tint-driven behavior. That is why subclassing is usually more explicit and controlled.
Decide Whether The Field Should Still Be Editable
Hiding the cursor is not the same as disabling editing.
Editable but cursorless:
Non-editable display-only field:
If the field should only display text and never accept input, disabling interaction is simpler than hiding the caret. The cursor question matters mainly when the field still needs to receive typed input or programmatic focus.
If You Also Want To Suppress Selection
Sometimes designers want a field that accepts input from a custom keypad or scanner but should not show selection menus or editing handles either.
You can disable editing actions in a subclass:
This is a stronger customization. Use it only when you really want to suppress normal text-editing affordances, because it changes expected platform behavior.
Common Use Cases
Hiding the cursor shows up in a few recurring designs:
- custom PIN or OTP input UIs
- scanner-driven input where typing is indirect
- text fields that proxy input from another control
- highly branded interfaces that replace the default editing appearance
These use cases can be valid, but they should still preserve clear user feedback in some other way.
For example, if the caret is hidden in a PIN entry control, users still need a visible indication of focus and input progress.
Accessibility And UX Considerations
A hidden cursor can make focus harder to understand, especially for users who expect normal iOS editing behavior. If you remove the caret, provide another signal such as:
- a highlighted border
- an active field background color
- segmented input boxes
- explicit instructional text
This matters because the caret is more than a decoration. It is part of how users understand where input will go.
So while the technical fix is simple, the design consequence is not trivial.
Common Pitfalls
The biggest mistake is hiding the caret when the real intent was simply to make the field non-editable. In that case, disabling the field or replacing it with a label is often clearer.
Another mistake is using tintColor = .clear without checking side effects on selection and editing UI. It may hide more than just the caret.
People also forget that hiding the cursor removes an important focus cue. If the field still accepts input, add another visible focus indicator.
Finally, if you use a subclass, make sure Interface Builder or code actually instantiates that subclass rather than a plain UITextField.
Summary
- The most reliable way to hide a
UITextFieldcursor is overridingcaretRect(for:)and returning.zero. - Setting
tintColorto clear can also hide the caret, but it is a broader visual change. - Hiding the cursor does not disable editing.
- If you suppress the caret, provide another clear focus indicator for users.
- Choose the simplest behavior that matches the real UX goal: editable without caret, or not editable at all.
Related reading
- Hiding the master view controller with UISplitViewController in iOS8
- ''Higher minimum deployment target'' error when installing Firebase Crash Reports Pods for iOS
- Hooking up UIButton to closure? Swift, target-action
- Horizontal ListView in Android?
- HorizontalScrollView within ScrollView Touch Handling
- How big should a UIBarButtonItem image be?
- How can a divider line be added in an Android RecyclerView?
- How can a web application send push notifications to iOS devices?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.