iOS 11
search bar
navigation bar
user interface
mobile development

Show search bar in navigation bar without scrolling on iOS 11

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

On iOS 11, UISearchController integration moved into the navigation item, which changed how and when the search bar appears. If you want the search bar visible without requiring the user to scroll, you need specific navigation bar settings. This guide shows a reliable setup and the most common issues that hide the search field unexpectedly.

Configure UISearchController in the Navigation Item

Start with a table-based view controller and attach a search controller to navigationItem.searchController.

swift
1import UIKit
2
3final class UsersViewController: UITableViewController, UISearchResultsUpdating {
4    private let searchController = UISearchController(searchResultsController: nil)
5    private let allUsers = ["Alice", "Bob", "Charlie", "Diana"]
6    private var filteredUsers: [String] = []
7
8    override func viewDidLoad() {
9        super.viewDidLoad()
10        title = "Users"
11
12        navigationItem.searchController = searchController
13        searchController.searchResultsUpdater = self
14        searchController.obscuresBackgroundDuringPresentation = false
15        searchController.searchBar.placeholder = "Search users"
16
17        definesPresentationContext = true
18        filteredUsers = allUsers
19    }
20
21    func updateSearchResults(for searchController: UISearchController) {
22        let text = searchController.searchBar.text ?? ""
23        if text.isEmpty {
24            filteredUsers = allUsers
25        } else {
26            filteredUsers = allUsers.filter { $0.localizedCaseInsensitiveContains(text) }
27        }
28        tableView.reloadData()
29    }
30
31    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
32        filteredUsers.count
33    }
34
35    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
36        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
37        cell.textLabel?.text = filteredUsers[indexPath.row]
38        return cell
39    }
40}

This setup gives you a search bar integrated into the large-title navigation experience.

Keep the Search Bar Visible Without Scrolling

The key property for iOS 11 behavior is hidesSearchBarWhenScrolling.

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3
4    navigationController?.navigationBar.prefersLargeTitles = true
5    navigationItem.largeTitleDisplayMode = .always
6    navigationItem.searchController = searchController
7
8    // Important: keep search bar visible at top without pull-down scroll.
9    navigationItem.hidesSearchBarWhenScrolling = false
10}

Set this after assigning the search controller. If it remains hidden, verify that your view controller is inside a UINavigationController and that you are not overriding navigation item state in another lifecycle method.

Handle Layout and Presentation Details

Search UI can behave oddly if presentation context is not set correctly. Keep definesPresentationContext = true so search presentation is scoped to the current controller.

When using segmented controls or custom headers, test with large and compact title modes because vertical spacing differs.

A minimal app bootstrap example:

swift
1import UIKit
2
3@main
4class AppDelegate: UIResponder, UIApplicationDelegate {
5    var window: UIWindow?
6
7    func application(
8        _ application: UIApplication,
9        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
10    ) -> Bool {
11        window = UIWindow(frame: UIScreen.main.bounds)
12        let usersVC = UsersViewController(style: .plain)
13        let nav = UINavigationController(rootViewController: usersVC)
14        window?.rootViewController = nav
15        window?.makeKeyAndVisible()
16        return true
17    }
18}

This creates a reproducible baseline for debugging search bar visibility.

Support iOS 11 Through Newer Versions Safely

If your app supports a wider iOS range, keep logic focused on stable APIs and avoid deprecated workarounds that manipulate private view hierarchy.

You can still keep the same visible search bar behavior on newer versions with the same navigationItem configuration. The main difference across versions is visual styling, not the core integration API.

Preserve Search State During Navigation

If users move to a detail screen and come back, keep search text and filtered results so context is not lost.

swift
1override func viewWillAppear(_ animated: Bool) {
2    super.viewWillAppear(animated)
3    navigationItem.searchController = searchController
4    navigationItem.hidesSearchBarWhenScrolling = false
5}

Persist the query string in a controller property or view model and re-run filtering in viewWillAppear when needed.

Common Pitfalls

A common issue is setting hidesSearchBarWhenScrolling on the wrong object or too early. Ensure it is set on navigationItem, not directly on UINavigationBar.

Another frequent problem is forgetting definesPresentationContext, which can cause odd dimming or presentation leaks into parent controllers.

Developers also debug in a standalone controller not embedded in navigation, then assume search API is broken. The search bar in this pattern depends on navigation controller ownership.

Summary

  • Assign UISearchController to navigationItem.searchController.
  • Set navigationItem.hidesSearchBarWhenScrolling = false to keep it visible.
  • Keep definesPresentationContext = true for correct presentation behavior.
  • Verify setup inside a UINavigationController.
  • Test in both large-title and standard-title modes to confirm consistent layout.

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.