UISearchBar
search text color
iOS development
Swift
UI customization

How can I change the UISearchBar search text color?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Changing the text color inside a UISearchBar really means changing the color of its embedded text field. In modern iOS code, the clean solution is to access searchTextField directly and set the textColor there.

Older examples often dig through subviews or use key-value coding. Those workarounds were common before Apple exposed a proper text field property, but they are no longer the first choice for current UIKit code.

The Modern Approach

On modern iOS versions, use searchBar.searchTextField:

swift
1import UIKit
2
3class ViewController: UIViewController {
4    let searchBar = UISearchBar()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        searchBar.searchTextField.textColor = .white
10        searchBar.searchTextField.tintColor = .white
11        searchBar.searchTextField.backgroundColor = .darkGray
12
13        view.addSubview(searchBar)
14    }
15}

textColor changes the typed text. tintColor affects the cursor and selection handles. In many designs, you want to set both.

Styling Placeholder Text Too

If you only change textColor, the placeholder may remain a different color. Use an attributed placeholder when you need consistent styling:

swift
1import UIKit
2
3let placeholder = NSAttributedString(
4    string: "Search",
5    attributes: [.foregroundColor: UIColor.lightGray]
6)
7searchBar.searchTextField.attributedPlaceholder = placeholder

That keeps the placeholder readable against custom backgrounds.

Appearance-Wide Customization

If your app uses many search bars with the same styling, you can centralize some of the configuration:

swift
1import UIKit
2
3let appearance = UISearchBar.appearance()
4appearance.searchTextField.textColor = .white
5appearance.searchTextField.backgroundColor = .black

Appearance APIs are helpful for global theming, but test carefully because a global rule affects every matching search bar in the app.

Supporting Older iOS Code

If you must support code that predates searchTextField, older projects sometimes retrieve the internal text field with key-value coding:

swift
if let textField = searchBar.value(forKey: "searchField") as? UITextField {
    textField.textColor = .white
}

Use that only when you truly need backward compatibility with legacy UIKit behavior. The direct searchTextField property is clearer and safer when available.

Working With UISearchController

Many modern screens embed the search bar through UISearchController rather than placing a bare UISearchBar directly in the view hierarchy. The styling rule is the same because you still customize the underlying searchTextField on the controller's search bar.

That consistency is useful because the same code path works whether the search UI sits in a navigation bar or in a standalone header.

Check Icons And Dark Mode Contrast

Text color is only one part of the visual result. If the search bar has a custom background, test the placeholder, magnifying-glass icon, clear button, and cursor together so the control remains readable in both light and dark appearances.

In practice, design bugs often come from styling the text field but forgetting that adjacent elements are still using default colors that no longer match the theme.

Common Pitfalls

The most common mistake is setting searchBar.tintColor and expecting the entered text color to change. On UISearchBar, the actual text is drawn by the internal text field, so you must change that field's textColor.

Another pitfall is changing the text color without checking contrast. A custom search bar background can make both placeholder text and entered text hard to read if only one of them is styled.

A third issue is relying on subview traversal by index. Internal UIKit view hierarchies are implementation details and can change across iOS versions. Prefer searchTextField when possible.

Summary

  • Change UISearchBar text color through searchBar.searchTextField.textColor.
  • Set tintColor too if you want the cursor and selection color to match.
  • Use an attributed placeholder when placeholder styling also matters.
  • Prefer the public searchTextField API over subview hacks or key-value coding.
  • Test text contrast after customization so the search field stays readable.

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