What is the bar in Apple Music called in SwiftUI to switch between "Apple Music" and "Your Library"?

Could you help me identify this and how to code it?

Thanks in advance!

Answered by darkpaw in 732485022

That's just an extra view with a segmented control In it. You can put the view between the search bar at the top, and the tableview below. If you want to hide it, set the height to 0.

Accepted Answer

That's just an extra view with a segmented control In it. You can put the view between the search bar at the top, and the tableview below. If you want to hide it, set the height to 0.

This is actually a feature that comes with searching called search scopes.

In SwiftUI, you would combine the searchable(text:placement:prompt:) modifier with searchScopes(_:scopes:) to achieve this effect.

Here's an example of how you would use it:

@State private var searchText = ""
@State private var selectedScope = "Option 1"

let allScopes = ["Option 1", "Option 2", "Option 3"]

...

.searchable(text: $searchText, prompt: "Search for something")
.searchScopes($selectedScope) {
    ForEach(allScopes, id: \.self) { scope in
        Text(scope)
    }
}


Check out the documentation for each modifier, and I would highly recommend reading this article for more on searching in SwiftUI.

What is the bar in Apple Music called in SwiftUI to switch between "Apple Music" and "Your Library"?
 
 
Q