![](/style/images/good.png)
![](/style/images/bad.png)
Always show search bar in a navigation bar in SwiftUI
source link: https://sarunw.com/posts/always-show-search-bar-in-swiftui/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Always show search bar in a navigation bar in SwiftUI
Table of Contents
In SwiftUI 3, we finally have a UISearchController equivalent in SwiftUI. But with a declarative nature, some setting that we can easily do in UIKit isn't obvious in SwiftUI. Today, I'm going to show you how to control search bar visibility on scrolling.
Default Behavior
By default, the search bar is hidden under the navigation title and visible only when a user pulls down the content.
![Left: The search bar hidden under the navigation title. Right: Pull down to reveal the search bar.](https://d33wubrfki0l68.cloudfront.net/bcb8f01274f82b84eeca9066afc1cdcc55b5da29/08e89/images/searchable-hides-searchbar-default.png)
When you scroll down, the search bar collapses into the navigation bar. This is the behavior for both UISearchControl in UIKit and searchable in SwiftUI.
UIKit
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
![Default behavior of navigation view with search control.](https://d33wubrfki0l68.cloudfront.net/8f5d10251fb71449f93c337787fd4f93c803729d/b8a5b/images/searchable-hides-searchbar-uikit.gif)
SwiftUI
NavigationView {
List {
...
}
.listStyle(.plain)
.navigationTitle("SwiftUI")
}
.searchable(text: $queryString) {
...
}
A plain list-style would yield the same behavior on SwiftUI.
![Default behavior of navigation view with search control in SwiftUI.](https://d33wubrfki0l68.cloudfront.net/64e4e48804c003cb6d419fbafbc659f66243193b/4ecae/images/searchable-hides-searchbar-swiftui.gif)
![revenucat2.jpg](https://d33wubrfki0l68.cloudfront.net/10ef326bb21f21a178f3b58c1265d719d7015e96/f9c41/images/sponsor/revenucat2.jpg)
Always Show Search Bar
To force the search bar to always show in UIKit, we set navigation item instance property hidesSearchBarWhenScrolling
to false
.
UIKit
With this simple change, the search bar will always show.
let searchController = UISearchController(searchResultsController: nil)
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.searchController = searchController
Search bar visible on first loaded and stay there even when users scroll down.
![hidesSearchBarWhenScrolling equals false make search bar always show.](https://d33wubrfki0l68.cloudfront.net/472b6cf4fe6413b5524d39a8f1675a57130586c6/9fcf4/images/searchable-hides-searchbar-show-uikit.png)
SwiftUI
To get this same behaviour in SwiftUI, we have to set placement
parameter in searchable modifier to .navigationBarDrawer(displayMode: .always)
.
NavigationView {
List {
...
}
.listStyle(.plain)
.navigationTitle("SwiftUI")
}
.searchable(text: $queryString, placement: .navigationBarDrawer(displayMode: .always)) {
...
}
Navigation Bar Drawer placement (.navigationBarDrawer
) tells SwiftUI that we want to place the search bar beneath the navigation bar title, and .always
display mode means we want it to stay there without collapse into the navigation bar.
With this change, you will get similar behavior as UIKit.
![.navigationBarDrawer(displayMode: .always)](https://d33wubrfki0l68.cloudfront.net/d1491572a8f59095f1627d413a98be763f3a3130/a8b71/images/searchable-hides-searchbar-show-swiftui.png)
Caveat
We have a minor behavior difference here.
- UIKit with
hidesSearchBarWhenScrolling = false
starts with a navigation bar in large title state. - SwiftUI with
.navigationBarDrawer(displayMode: .always)
starts with a navigation bar in a inline title state. You still get a large title when you pull the content down. The only difference is the initial state of the title.
You may also like
SwiftUI finally got native search support in iOS 15. We can add search functionality to any navigation view with the new searchable modifier. Let's explore its capability and limitation.
SwiftUI searchableSwiftUI provides an easy way to convert to render any view into a placeholder style by redacting its content.
SwiftUIThe last part in a series on understanding data in SwiftUI. See all tools SwiftUI provided to declare different types of data and dependency. Learn when and how to use @State, @Binding, ObservableObject, @ObservedObject, @EnvironmentObject, and @Environment.
SwiftUIRead more article about SwiftUI, searchable,
or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
4 Xcode shortcuts to get back your screen space
Working on your MacBook without an external monitor can be troublesome due to the small screen size. I will show you 4 Xcode shortcuts that might mitigate the situation.
Dynamic button configuration in iOS 15
Learn how to change button configuration, e.g., title and color, based on the internal and external changes.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK