This is the final article in our series on customizing the appearance of the NavigationBar
and TabBar
. In this post, we’ll go a step further and show how to change the default color of the tab badge which is red by default.
As you might expect, we’ll need a few more Declares to accomplish this. Continue reading to learn how!
Since we’re building on the example project from the first blog post in this series, I strongly recommend completing that post if you haven’t already, then continuing with the second and third articles before proceeding here.

Select the “DeclaresForiOS” module and add a new method to it, using the following values for its signature in the Inspector Panel:
- Method Name:
TabBarBadgeColor
- Parameters: Extends tab As
iOSTabBar
, Assigns value AsColorGroup
- Scope: Global
And put the following snippet of code in the associated Code Editor:
Var tabController As Ptr = tab.ViewControllerHandle
If value = Nil Then Return
Declare Function GetTabBar Lib "UIKit" Selector "tabBar" (obj As Ptr) As Ptr
Var tabBar As Ptr = GetTabBar(tabController)
Var colorPtr As Ptr = ColorGroupToUIColor(value)
Declare Function BarAppearance Lib "UIKit" Selector "standardAppearance" (obj As Ptr) As Ptr
Declare Sub SetAppearance Lib "UIKit" Selector "setStandardAppearance:" (obj As Ptr, value As Ptr)
Var appearance As Ptr = BarAppearance(tabBar)
Declare Function StackedLayout Lib "UIKit" Selector "stackedLayoutAppearance" (obj As Ptr) As Ptr
Declare Function InlineLayout Lib "UIKit" Selector "compactInlineLayoutAppearance" (obj As Ptr) As Ptr
Declare Function NormalState Lib "UIKit" Selector "normal" (obj As Ptr) As Ptr
Var stackLayout As Ptr = StackedLayout(appearance)
Var inLayout As Ptr = InlineLayout(appearance)
Var normalState As Ptr = NormalState(stackLayout)
Var normalInlineState As Ptr = NormalState(inLayout)
Declare Sub SetBadgeColor Lib "UIKit" Selector "setBadgeBackgroundColor:" (obj As Ptr, value As Ptr)
SetBadgeColor(normalState, colorPtr)
SetBadgeColor(normalInlineState, colorPtr)
SetAppearance(tabBar, appearance)
If System.Version.MajorVersion >= 15 Then
Declare Sub SetScrollEdgeAppearance Lib "UIKit" Selector "setScrollEdgeAppearance:" (obj As Ptr, value As Ptr)
SetScrollEdgeAppearance(tabBar, appearance)
End If
Next, select the Screen1 item in the Navigator and add a new property to it using the following values in the Inspector Panel:
- Name:
MyTabBadgeColor
- Type:
ColorGroup
- Scope: Protected
Setting the Badge Text Color
What’s next? I’m sure you guessed it! Now that we can customize the badge color, the default white text color on the badge may not always provide the best contrast.
Let’s add a new method to our “DeclaresForiOS” module with the following signature values in the Inspector Panel:
- Name:
TabBarBadgeTextColor
- Parameters: Extends tab As
iOSTabBar
, Assigns value AsColorGroup
- Scope: Global
And type (or paste) the following snippet of code in the associated Code Editor:
Var tabController As Ptr = tab.ViewControllerHandle
If value = Nil Then Return
Declare Function GetTabBar Lib "UIKit" Selector "tabBar" (obj As Ptr) As Ptr
Var tabBar As Ptr = GetTabBar(tabController)
Var colorPtr As Ptr = ColorGroupToUIColor(value)
Declare Function BarAppearance Lib "UIKit" Selector "standardAppearance" (obj As Ptr) As Ptr
Declare Sub SetAppearance Lib "UIKit" Selector "setStandardAppearance:" (obj As Ptr, value As Ptr)
Var appearance As Ptr = BarAppearance(tabBar)
Declare Function StackedLayout Lib "UIKit" Selector "stackedLayoutAppearance" (obj As Ptr) As Ptr
Declare Function InlineLayout Lib "UIKit" Selector "compactInlineLayoutAppearance" (obj As Ptr) As Ptr
Declare Function NormalState Lib "UIKit" Selector "normal" (obj As Ptr) As Ptr
Var stackLayout As Ptr = StackedLayout(appearance)
Var inLayout As Ptr = InlineLayout(appearance)
Var normalState As Ptr = NormalState(stackLayout)
Var normalInlineState As Ptr = NormalState(inLayout)
Declare Sub SetBadgeTextAttr Lib "UIKit" Selector "setBadgeTextAttributes:" (obj As Ptr, value As Ptr)
Declare Function NSClassFromString Lib "Foundation" (name As CFStringRef) As Ptr
Declare Function DictionaryWithObjectForKey Lib "UIKit" Selector "dictionaryWithObject:forKey:" (obj As Ptr, value As Ptr, key As CFStringRef) As Ptr
Var dict As Ptr = DictionaryWithObjectForKey( NSClassFromString("NSMutableDictionary") , colorPtr, "NSColor")
SetBadgeTextAttr(normalState, dict)
SetBadgeTextAttr(normalInlineState, dict)
SetAppearance(tabBar, appearance)
If System.Version.MajorVersion >= 15 Then
Declare Sub SetScrollEdgeAppearance Lib "UIKit" Selector "setScrollEdgeAppearance:" (obj As Ptr, value As Ptr)
SetScrollEdgeAppearance(tabBar, appearance)
End If
Next, select the Screen1
item in the Navigator and add a new property to it using the following values in the Inspector Panel:
- Name:
MyTabBadgeTextColor
- Type:
ColorGroup
- Scope: Protected
Then, select the Screen1.Opening
event handler and add these lines of code to it:
MyTabBadgeColor = New ColorGroup(Color.Green, Color.Blue)
MyTabBadgeTextColor = New ColorGroup(Color.Black, Color.Yellow)
Me.ParentTabBar.BadgeAt(0) = "33"
Me.ParentTabBar.BadgeAt(1) = "42"
Lastly, select the Screen1.AppearanceChanged
event handler and add this line of code at the end:
Me.ParentTabBar.TabBarBadgeColor = MyTabBadgeColor
Me.ParentTabBar.TabBarBadgeTextColor = MyTabBadgeTextColor
Testing the App
Everything is set! Run the app on your iPhone or in the Simulator, and you’ll see each tab in the TabBar
displaying its own badge with the custom background and text colors we configured in the Opening Event.
Download the example project from this link.
Happy Xojo Coding!
Javier Menendez is an engineer at Xojo and has been using Xojo since 1998. He lives in Castellón, Spain and hosts regular Xojo hangouts en español. Ask Javier questions on Twitter at @XojoES or on the Xojo Forum.