TabBar icon badge

  1. 7 months ago

    Olivier C

    6 May 2019 Pre-Release Testers, Xojo Pro Europe (Belgium, France)
    Edited 7 months ago

    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:

    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

    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 !

    Hi @Jeremie L, 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 !

  2. Jeremie L

    6 May 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Europe (France) - getpackr.io

    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)
  3. Olivier C

    6 May 2019 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

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

  4. Norman P

    6 May 2019 Pre-Release Testers, Xojo Pro Outside Innisfail

    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)

  5. Olivier C

    6 May 2019 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

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

    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 ?

  6. Jeremie L

    6 May 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Europe (France) - getpackr.io

    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.

  7. Olivier C

    7 May 2019 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

    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.

  8. Olivier C

    7 May 2019 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

    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.

  9. Jeremie L

    7 May 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Europe (France) - getpackr.io
    Edited 7 months ago

    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.

  10. Olivier C

    7 May 2019 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

    Hi Jeremie,

    @Jeremie L 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.

    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:
    -image-
    Device:
    -image-

  11. Olivier C

    8 May 2019 Pre-Release Testers, Xojo Pro Answer Europe (Belgium, France)
    Edited 7 months ago

    Hi @Jeremie L, 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 !

  12. Jeremie L

    8 May 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Europe (France) - getpackr.io

    I'm glad it works now!

or Sign Up to reply!