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.
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.
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.
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:
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.
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
UISearchControllertonavigationItem.searchController. - Set
navigationItem.hidesSearchBarWhenScrolling = falseto keep it visible. - Keep
definesPresentationContext = truefor correct presentation behavior. - Verify setup inside a
UINavigationController. - Test in both large-title and standard-title modes to confirm consistent layout.
Related reading
- Sign APK without putting keystore info in build.gradle
- Silent Push Notification in iOS 7 does not work
- Silent pushes not delivered to the app on iOS 11
- Simple and clean way to convert JSON string to Object in Swift
- Simple and clean way to convert JSON string to Object in Swift
- Simple Android grid example using RecyclerView with GridLayoutManager like the old GridView
- Simple Android RecyclerView example
- Simplest way to throw an error/exception with a custom message in Swift?
.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.