Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Badge color on TabItem

Tags:

swiftui

badge

I need to change the color of a tabItem badge in SwiftUI. I founded this pod https://github.com/jogendra/BadgeHub

Is it possible to use with SwiftUI?

I have tabItems:

TabView(selection: $selection) {
    FeaturedView(users: filteredUsers, usersCount: filteredUsersCount)
        .tabItem {
            Image(selection == 0 ? "ic_searchFill" : "ic_search").renderingMode(.template)
        }
        .tag(0)
    LikesView(userViews: self.userViews, userLikes: self.userLikes)
        .tabItem {
            Image(selection == 1 ? "ic_heartFill" : "ic_heart").renderingMode(.template)
        }
        .badge(userLikesCount)
        .tag(1)
    ConversationsView(conversations: conversations)
        .tabItem {
            Image("ic_chat").renderingMode(.template)
        }
        .badge(conversationsCount)
        .tag(2)
    SettingsView(user: user)
        .tabItem {
            Image(selection == 3 ?  "ic_profileFill" : "ic_profile").renderingMode(.template)
        }
        .tag(3)
}
like image 638
user14300555 Avatar asked Oct 28 '25 00:10

user14300555


1 Answers

Actually we can configure SwiftUI badge color as well, via appearance.

Demo

Tested with Xcode 13.4 / iOS 15.5

init() {
    UITabBarItem.appearance().badgeColor = .purple   // << here !!
}

var body: some View {
    TabView {
        Color.green.overlay(Text("One"))
            .tabItem { Image(systemName: "car") }
            .badge(4)
// ...
like image 173
Asperi Avatar answered Oct 29 '25 18:10

Asperi