Swift
UITextField
iOS Development
Image Icon
SwiftUI

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.

Browse interview questions

Introduction

Adding an icon inside a UITextField is a common iOS UI pattern for search, email, password, and phone inputs. The standard approach is to assign a custom view to leftView or rightView and set the corresponding view mode. This keeps layout stable and avoids manual drawing hacks.

Most visual bugs come from incorrect padding, missing constraints on custom icon containers, or forgetting to set leftViewMode so the icon never appears.

Core Sections

1. Add a left icon with padding

swift
1import UIKit
2
3func configureIconField(_ textField: UITextField) {
4    let icon = UIImageView(image: UIImage(systemName: "magnifyingglass"))
5    icon.tintColor = .secondaryLabel
6    icon.contentMode = .scaleAspectFit
7
8    let container = UIView(frame: CGRect(x: 0, y: 0, width: 34, height: 20))
9    icon.frame = CGRect(x: 10, y: 0, width: 16, height: 20)
10    container.addSubview(icon)
11
12    textField.leftView = container
13    textField.leftViewMode = .always
14    textField.placeholder = "Search"
15}

This gives predictable spacing between icon and text.

2. Add right-side action icon

swift
1let eye = UIButton(type: .system)
2eye.setImage(UIImage(systemName: "eye"), for: .normal)
3eye.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
4textField.rightView = eye
5textField.rightViewMode = .whileEditing

Useful for password visibility toggles.

3. Customize text inset if needed

For advanced spacing, subclass UITextField:

swift
1class PaddedTextField: UITextField {
2    override func textRect(forBounds bounds: CGRect) -> CGRect {
3        bounds.insetBy(dx: 8, dy: 0)
4    }
5
6    override func editingRect(forBounds bounds: CGRect) -> CGRect {
7        bounds.insetBy(dx: 8, dy: 0)
8    }
9}

Combine with left/right accessory views for full control.

4. Handle dynamic type and dark mode

Use semantic colors (.secondaryLabel) and SF Symbols where possible so icon appearance adapts to theme and accessibility settings.

5. Reuse configuration helpers

Encapsulate field styling in a helper or view component so all forms keep consistent icon alignment and spacing.

Common Pitfalls

  • Setting leftView but forgetting leftViewMode, so icon never appears.
  • Using icon frames that clip on smaller text field heights.
  • Hardcoding colors that fail dark-mode contrast requirements.
  • Mixing Auto Layout and manual frames without clear ownership.
  • Repeating icon setup logic across screens and introducing inconsistent UI.

Summary

To add an icon in UITextField, assign a padded leftView or rightView and set its display mode explicitly. Use semantic styling for accessibility and theme support, and encapsulate configuration logic for reuse. With structured accessory views and spacing control, text-field icons become clean, consistent, and easy to maintain.

A practical way to make this guidance durable is to convert it into a small runbook that includes prerequisites, expected environment versions, and a short verification sequence. Even strong teams lose time when troubleshooting steps live only in memory or chat history. A runbook should explicitly answer three questions: what to check first, what output confirms healthy behavior, and what output indicates a known failure mode. This level of clarity helps both experienced maintainers and newer contributors, and it reduces repeated investigation during incidents.

It is also valuable to create a tiny reproducible fixture for this topic. The fixture can be a minimal script, test case, sample request, or small dataset that demonstrates the correct behavior in isolation. When regressions appear after dependency upgrades, infrastructure changes, or framework migrations, that fixture becomes the fastest way to isolate whether the issue is environmental or logic-related. Keeping a focused fixture in source control gives you a stable benchmark across branches and release cycles.

For long-term reliability, pair documentation with one automated guardrail in CI. The guardrail should be narrow and fast: an import check, schema validation, endpoint contract test, deterministic unit test, or lightweight performance threshold. Avoid broad flaky checks that hide real signals. The goal is early, actionable feedback before code reaches production. If the same category of issue appears repeatedly, promote the manual troubleshooting step into automation so the system catches it first. Over time, this shifts effort from reactive debugging to preventive quality control and keeps the knowledge article relevant in real engineering workflows.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.