TabBar icon badge

Hi,

I want to put a badge on some TabBar icon (the little red dot with a number).

I found multiple post, e.g. using

EDIT: full method:

[code]Public Sub setTabBarBadge(extends view As iOSView, index As Integer, assigns badge As Text)
Dim tb As iOSTabBar=view.ParentTabBar
If tb Is Nil Then Return
Dim h As ptr=tb.ViewControllerHandle

Declare Function tabbar_ Lib “UIKit” selector “tabBar”(o As Ptr) As Ptr
Dim tabbar As Ptr=tabbar_(h)

Declare Function items_ Lib “UIKit” selector “items”(o As Ptr) As Ptr
Dim items As Ptr=items_(tabbar)

Declare Function count_ Lib “foundation” selector “count”(o As Ptr) As Integer
Dim count As Integer=count_(items)
If index<count Then
Declare Function objAtIndex Lib “foundation” selector “objectAtIndex:”(o As Ptr, idx As Integer) As Ptr
Dim item As Ptr=objAtIndex(items,index)

Declare Sub setBadgeValue Lib "UIKit" selector "setBadgeValue:" (o As ptr, value As cfstringref)
setBadgeValue item, badge

End If

End Sub
[/code]

I also tried with Jeremie Leroy extensions.

Everything works fine in the simulator, I can place a badge (and remove it) on any tab icon, using the tab index, great !

But when I install on a physical device the badge is always on the first tab icon…

Any idea ?

Thanks !

Maybe this will work better, note the UInteger instead of Integer.

Declare Function objAtIndex Lib "foundation" selector "objectAtIndex:"(o As Ptr, idx As UInteger) As Ptr Dim item As Ptr=objAtIndex(items,index)

Merci Jeremie.
I’ll try tomorrow and let you know.

Uinteger and Integer are the same size
Only different is one is treated as unsigned vs signed (but the underlying bit patterns can be identical)

[quote=435257:@Jeremie Leroy]Maybe this will work better, note the UInteger instead of Integer.

[/quote]
Unfortunately, it doesn’t solve the problem.
It’s still OK when running in the simulator but the badge stays on first tab icon when running on the physical devices.
I’m testing on iPhone SE and iPad Air, both on iOS 12.2.

The tabbar is created in the IDE, not by code, could that be the reason ?

Do you call the setTabBarBadge function in iOSView.Open event?
If you do, try in iOSView.Activate event.

If you call it afterwards when the view is already created, it shouldn’t change anything.

It’s called in a timer on the main view.
The badge is changing or turned to none correctly it’s just on the wrong icon.
Maybe I need to wait that all tab related views are open.

Edit: changed to activate the timer in the activate event of the main view (rather than open event).
Now the tab icon badge is placed on 2nd icon.

The badge is still and always on the 3rd icon, as required, when running in the simulator.

What is the delay of the timer? Maybe it is too short.

If you place a button in a view to set the tabbar icon does it set it correctly on 3rd icon?
This is just to verify that code works as expected, as pressing the button can only be done when all views are loaded.

Hi Jeremie,

[quote=435338:@Jeremie Leroy]What is the delay of the timer? Maybe it is too short.

If you place a button in a view to set the tabbar icon does it set it correctly on 3rd icon?
This is just to verify that code works as expected, as pressing the button can only be done when all views are loaded.[/quote]

The timer has a period of 5 secs (5000), it’s used to check server connection.

I disabled the timer and tried placing a button on main view, setting the badge on tab icon 3.
No luck: still ok in the simulator, but always placing the badge on 2nd icon on real device.

I tried the following code in the button action:

For i As Integer = 0 To 4 Self.setTabBarBadge(i) = i.ToText Next
In the simulator, tabbar icons are each receiving a badge numbered 0 to 4
On the device, only the 2nd icon is receiving 4, i.e. all badges are placed on the same icon, the last one remaining.
Simulator:

Device:

Hi @Jeremie Leroy, I re-downloaded your iOSDesignExtensions and re-used your SetTabBarBadgeXC, as is.
And it works !

I just changed to suppress the badge when empty:

If Badge = "" Then
  setBadgeValue(item0, Nil)
Else
  setBadgeValue(item0, Badge)
End If

I’ve no time now to find out where the other method fails.

I wanted to thank you for you support, and for your tools !

Thanks !

I’m glad it works now!