27

I'm attempting to add a badge alert label like the one in the screenshot attached.

enter image description here

I've tried to search for titles, labels uitabbar items but I'm stuck.

Any suggestion is appreciated.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Gino
  • 951
  • 1
  • 12
  • 24
  • Do you want to refer to the red badge with the white ‘1’? “an alert label” isn’t really specific ;) – milo526 Apr 18 '15 at 16:36
  • Haha, I thought so too, but that's the best way I can think of to describe it. Yes, that red badge with the 1 is what I want. – Gino Apr 18 '15 at 16:53
  • Sorry been very hectic lately at work, I'll take a look once I have the time and upvote it. Cheers – Gino Apr 24 '15 at 11:32

1 Answers1

67

Xcode 7.2.1 Swift 2.1.1

You just have to set the badgeValue for your desired UITabBarItem as follow:

tabBarController?.tabBar.items?[4].badgeValue = "1"   // this will add "1" badge to your fifth tab bar item


// or like this to apply it to your first tab
tabBarController?.tabBar.items?.first?.badgeValue = "1st"

// or to apply to your second tab
tabBarController?.tabBar.items?[1].badgeValue = "2nd"

// to apply it to your last tab
tabBarController?.tabBar.items?.last?.badgeValue = "Last"

To remove a badge from the UITabBarItem just add nil value to it

tabBarController?.tabBar.items?.first?.badgeValue = nil
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571