I got 3 views, the main view is MyTabView, and I would like to hide the TabBar when I navigate to a subview, and I define a class to store config which called TabBarConfig, and I pass it to subviews using EnvironmentObject. But when I change the data inside the class in subview, the TabBar doesn't hide, and it shows a purple warning read that "Accessing StateObject's object without being installed on a View. This will create a new instance each time", Here's the codes:
class TabBarConfig: ObservableObject {
@Published var hideTabBar: Bool = false
}
struct MyTabView: View {
@ObservedObject var tabBarConfig = TabBarConfig()
@State private var selectedItem = 0
init() {
UITabBar.appearance().isHidden = self.tabBarConfig.hideTabBar
}
var body: some View {
TabView(selection: $selectedItem) {
HomeView()
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
.tag(0)
UserView()
.tabItem {
Image(systemName: "person.fill")
Text("User")
}
.tag(1)
}
.environmentObject(self.tabBarConfig)
}
}
struct HomeView: View {
@EnvironmentObject var tabBarConfig: TabBarConfig
@State var showSubView: Bool = false
var body: some View {
NavigationView {
NavigationLink(destination: SubView(), isActive: $showSubView) {
Button(action: {
showSubView = true
}, label: {
Text("Go To SubView")
})
}
.environmentObject(self.tabBarConfig)
}
}
}
struct SubView: View {
@EnvironmentObject var tabBarConfig: TabBarConfig
var body: some View {
VStack {
Text("Some Text")
}
.onAppear {
self.tabBarConfig.hideTabBar = true
}
}
}
Hope you can help me out.