UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: .searchable behavior change? Day 96

Forums > 100 Days of SwiftUI

Hi all,

In Day 96, Paul implements a .searchable search bar that shows ideal behavior (imo) - the search bar stays hidden until the user pulls down slightly on the view, causing the search bar to appear to slide out from under the navigation bar into view.

I noticed that this behavior does not seem to still be present in the current version of Swift - when using .searchable, the search bar is always visible. A forum post from about a year ago seems to note this as well, but I'm having trouble confirming this elsewhere. Am I crazy, does this behavior still exist?

Paul's code, in case you'd like to test it yourself:

struct ContentView: View {
    @State private var searchText = ""
    let allNames = ["Subh", "Vina", "Melvin", "Stefanie"]

    var body: some View {
        NavigationView {
            List(filteredNames, id: \.self) { name in
                Text(name)
            }
            .searchable(text: $searchText, prompt: "Look for something")
            .navigationTitle("Searching")
        }
    }

    var filteredNames: [String] {
        if searchText.isEmpty {
            return allNames
        } else {
            return allNames.filter { $0.contains(searchText) }
        }
    }
}

Thanks!

2      

Quick answer Yes. Think they have changed it to make the search more accessible (do not quote me on that). However you can change it to alway show with by add placement to .always this will not hid it when scrolling.

searchable(text:placement:prompt:)

2      

Hey Nigel! Thanks as always for taking a look :) I'm actually looking for the opposite behavior - I want the search bar to be hidden until you pull down (see 3:20 in Paul's video linked above). It's a minor visual annoyance if anything, but I was curious if I'm doing something wrong or if the searchable visibility really has changed.

2      

Yes, search bar has changed

There are a number of options for placement depending on platform. Check out the Document in link above.

However in iOS it mainly down to two.

  1. .automatic (This is default) - This will show the search bar then hid it when scrolling. This will also change depending on platform and size class.
  2. .always - This will show search bar always even when scrolling.

My guess is that Apple may of thought that the search bar was to hidden and user did not know about it, and therefore make more available 🤷‍

2      

I thought I was going nuts with this problem! Glad to see others have run into it, too.

My app's main UI is a TabView, and oddly the searchbar is still hidden by default on the first tabview tab displayed, but not the others. And it's not hidden on the first tabview tab consistently. Not happy with the change...I wonder whether this is a bug in the searchable code.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.