Swift add icon/image in UITextField
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Adding an icon to a UITextField is usually done with the built-in leftView or rightView properties. The main implementation detail is not the image itself, but wrapping it in a container view so the field gets proper padding and the icon does not sit flush against the edge.
Use leftView for the Most Common Case
The standard pattern is to create an image view, place it inside a small container, and assign that container to leftView.
The container provides padding, which is why this looks better than assigning the image view directly.
Use rightView for Clear Buttons or Status Icons
The same idea works on the right side.
This is useful for validation indicators, password visibility buttons, and accessory actions.
Make the Icon Interactive Only If It Needs To Be
If the image is purely decorative, a plain image view is enough. If tapping it should trigger an action, use a button instead.
That is cleaner than adding gesture recognizers to an image view when the icon is actually meant to behave like a control.
Keep Layout Consistent Across Fields
If several text fields use icons, extract the setup into a helper to keep padding and sizing consistent.
That makes the form feel intentional instead of slightly different on each field.
UIKit Versus SwiftUI
Even though the tag list mentions SwiftUI, this topic is really about UIKit's UITextField. In SwiftUI, you would typically build the effect differently, often by composing an HStack with an Image and a TextField, or by wrapping a UIKit text field in UIViewRepresentable if you need UIKit-specific behavior.
So the API answer here is specifically leftView and rightView on UITextField.
Common Pitfalls
The most common mistake is assigning an image view directly without a container and ending up with no padding.
Another common issue is forgetting to set leftViewMode or rightViewMode, which makes the icon never appear. Developers also often use a decorative image view when the icon should actually be a button, which makes the UI harder to interact with correctly.
Summary
- Use
leftVieworrightViewto place icons inside aUITextField. - Wrap the icon in a container view so the field has proper padding.
- Use
leftViewModeorrightViewModeto control when the icon is visible. - Use a button instead of an image view when the icon should be tappable.
- Extract common setup into helpers so multiple fields stay visually consistent.
Related reading
- Swift add icon/image in UITextField
- Swift addsubview and remove it
- Swift Alamofire How to get the HTTP response status code
- Swift Alamofire VS AFNetworking
- Swift alert view with OK and Cancel which button tapped?
- Swift and mutating struct
- Swift apply .uppercaseString to only the first letter of a string
- Swift apply .uppercaseString to only the first letter of a string
.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.