I am learning about swiftUI navigation stack and Tab Bars, most tutorials just show implementation of the tab bars. i am trying to hide my custom tab bar in subviews. Please can someone explain what to do so that I can learn and take note of it for future references. Thanks in advance
struct MainTabbedView: View {
@State var selectedTab = 0
@Environment(\.isTabBarVisible) var isTabBarVisible
var body: some View {
ZStack(alignment: .bottom) {
TabView(selection: $selectedTab) {
HomeView()
.tag(0)
FavoriteView()
.tag(1)
FantasyView_Preview()
.tag(2)
ProfileView()
.tag(3)
}
if isTabBarVisible {
ZStack {
HStack {
ForEach((TabbedItems.allCases), id: \.self) { item in
Button {
selectedTab = item.rawValue
} label: {
CustomTabItem(imageName: item.iconName, title: item.title, isActive: (selectedTab == item.rawValue))
}
}
}
.padding(6)
}
.frame(height: 70)
.background(Color.yinmnBlue.opacity(0.2))
.cornerRadius(35)
.padding(.horizontal, 26)
}
}
extension MainTabbedView {
func CustomTabItem(imageName: String, title: String, isActive: Bool) -> some View {
HStack(spacing: 10) {
Spacer()
Image(systemName:imageName)
.resizable()
.renderingMode(.template)
.foregroundColor(isActive ? .black : .gray)
.frame(width: 25, height: 25)
if isActive {
Text(title)
.font(.system(size: 14))
.foregroundColor(isActive ? .black : .gray)
}
Spacer()
}
.frame(width: isActive ? .infinity : 60, height: 60)
.background(isActive ? Color.yinmnBlue.opacity(0.4) : .clear)
.cornerRadius(30)
}
}
HomeView.swift
struct HomeView: View {
@State private var showsubview = false
var body: some View {
NavigationStack{
Button(action: {
showsubview = true
}, label: {
Text("Go to sub view")
})
.navigationDestination(isPresented: $showsubview) {
navigation1(isPresented: $showsubview)
.environment(\.isTabBarVisible, false)
}
}
}
}
i tried to use environment variables but it did not work
struct TabBarVisibilityKey: EnvironmentKey {
static let defaultValue: Bool = true
}
extension EnvironmentValues {
var isTabBarVisible: Bool {
get { self[TabBarVisibilityKey.self] }
set { self[TabBarVisibilityKey.self] = newValue }
}
}
navigation1(isPresented: $showsubview)
? Where isMainTabbedView
called?