I am learning TabView and have a requirement where my main/content view will display map and will have TabView at the bottom. Whenever any of the tab's on TabView is tapped, a custom modal sheet (with slider handle to adjust sizes... more like bottom sheet) will be opened. Whenever I slide the modal down completely or close the modal sheet, the selected tab on tabview should get de-selected i.e. no tabs on the TabView should be selected by default.
Is this achievable with SwiftUI TabView? I am noticing first tab is always selected by default. Or will I have to create a custom tab view to achieve this?
Code:
import SwiftUI
import Foundation
struct ContentView: View {
var body: some View {
VStack {
SimpleDataView()
BottomTabView()
}
}
}
struct SimpleDataView: View {
var body: some View {
VStack {
Text("Map will be displayed over here").background(Color.gray).frame(maxWidth: .infinity, maxHeight: .infinity)
Spacer()
}
.background(Color.gray)
.ignoresSafeArea()
}
}
struct BottomTabView: View {
@State var selection: Int = -4
var body: some View {
TabView(selection: $selection) {
Group {
/// Custom modal view with slider handle/bottom sheet will be displayed.
Text("Home tab's view")
.tabItem {
Label("Home", systemImage: "house")
}
.tag(0)
.frame(height: 100.0)
Text("Search tab's view")
.tabItem {
Label("Search", systemImage: "magnifyingglass")
}
.tag(1)
.frame(height: 100.0)
}
}.onAppear {
UITabBar.appearance().backgroundColor = .lightGray
}
}
}
To keep the code short and simple, I am using Text() element in my BottomTabView struct for now, but it will be replaced with CustomModalSheet which when closed, so de-select the selected tab on tab view and no tabs should be selected by default.