I have a TabView. In the TabView are two views - HomePageView and StatsPageView. the HomePageView is the first view to load. when the home page loads the tabbar has a white background instead of the pink background set in the init() method. when I go to the StatsPageView the TabBar is pink. when I go back to the home view the TabBar is pink. why is it loading white instead of pink? I have tried putting the UITabBar.appearance().barTintColor = .systemPink
in the ContentView init() method, I've tried putting in an .onAppear()
on the TabView itself, I have even tried building an extension for the UITabBarController. all did the exact same thing. weirdly enough
here's a gif:
https://giphy.com/gifs/O0aPkQfTXtn40WpFW1
here's the code for ContentView()
struct ContentView: View {
@State private var selectedTab = 0
let numTabs = 2
let minDragTranslationForSwipe: CGFloat = 50
init() {
UITabBar.appearance().barTintColor = .systemPink
// this does nothing. does not set on first tab but does set second tab
}
var body: some View {
TabView(selection: $selectedTab) {
HomeView()
.tabItem {
Label("Home", systemImage: "house.fill")
}.tag(0)
.highPriorityGesture(DragGesture().onEnded({ self.handleSwipe(translation: $0.translation.width)}))
StatsView()
.environment(\.managedObjectContext, CoreDataStack.shared.viewContext)
.tabItem {
Label("Profile", systemImage: "square.and.pencil")
}.tag(1)
.highPriorityGesture(DragGesture().onEnded({ self.handleSwipe(translation: $0.translation.width)}))
}
.onAppear(){
UITabBar.appearance().barTintColor = .systemPink
// this does NOT set on first tab but does when you go to the second tab and then sets on first tab when you go back
print("in onAppear")
}
.accentColor(Color.green)
// weirdly enough this sets onload on the first view
}
private func handleSwipe(translation: CGFloat) {
if translation > minDragTranslationForSwipe && selectedTab > 0 {
selectedTab -= 1
} else if translation < -minDragTranslationForSwipe && selectedTab < numTabs-1 {
selectedTab += 1
}
}
}