0

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
    }
}
}
zamanada
  • 158
  • 13

1 Answers1

0

If you want to set the TabBar background, then you should use:

init() {
    UITabBar.appearance().backgroundColor = .systemPink
}

The UITabBar.appearance().tintColor is the color of the items, which in your code, would color:

Label("Home", systemImage: "house.fill") and 
Label("Profile", systemImage: "square.and.pencil")

Lastly, UITabBar.appearance().barTintColor is used to replace what is provided by the barStyle property.

Yrb
  • 8,103
  • 2
  • 14
  • 44
  • yeah I caught that once I posted it. I went and fixed that but still had the same result. ended up just giving up on a custom colored tab bar – zamanada Jan 31 '22 at 05:05
  • I had no error when I substituted it into your posted code. You may have an issue somewhere else. – Yrb Jan 31 '22 at 15:08
  • excellent. thanks for that. I figured that was the case but my to do list on this project is so long I didn't want to stop too long on any one thing. if nothing else, you confirming that saves me some time. – zamanada Jan 31 '22 at 17:54