Using SwiftUI, how can I achieve something like the Apple Music app? they have a now playing bar that is visible across all tabs. Is this something I can recreate using SwiftUI?
I have tried using overlay:
TabView {
NavigationStack(path: $coordinator.path) {
ContentView()
.navigationDestination(for: Destination.self) { destination in
ViewFactory.viewForDestination(destination)
}
}
.tabItem {
Text("Home")
}
DownloadsView()
.tabItem {
Text("Downloads")
}
}
.overlay(alignment: .bottom) {
MediaControlBar()
}
but the bar overlaps the tabs and does not adjust the content of the views
EDIT:
I am a bit closer with this custom implementation, but it will not let me switch views:
struct MyTabView: View {
var items: [MyTabItem]
@State var selectedItem: MyTabItem
var body: some View {
VStack {
selectedItem.view
MediaControlBar()
Picker("tabs", selection: $selectedItem) {
ForEach(items) { item in
Text(item.title)
}
}
.pickerStyle(.segmented)
}
}
struct MyTabItem: Hashable, Equatable, Identifiable {
static func == (lhs: MyTabView.MyTabItem, rhs: MyTabView.MyTabItem) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
let id = UUID()
let view: AnyView
let title: String
}
}