How to set tab bar icon fast?

How to set tab icon with quick? For example, when I receive a new message with number 1 on the message icon! Should I use UITabBarItem.swift and write code in it! I am not sure how to do this.

Thanks!

+6
source share
3 answers

If you got a link to tabBarController (e.g. from a UIViewController), you can do the following:

if let tabItems = self.tabBarController?.tabBar.items as NSArray!
{
    // In this case we want to modify the badge number of the third tab:
    let tabItem = tabItems[2] as! UITabBarItem
    tabItem.badgeValue = "1"
}

From the UITabBarController, it will be:

// Access the elements (NSArray of UITabBarItem) (tabs) of the tab Bar
let tabItems = self.tabBar.items as NSArray!

// In this case we want to modify the badge number of the third tab:
let tabItem = tabItems[3] as! UITabBarItem

// Now set the badge of the third tab
tabItem.badgeValue = "1"

and remove the icon:

tabItem.badgeValue = nil
+16
source

The following line can help you show the icon in UITabBerItem

tabBarController?.tabBar.items?[your_desired_tabBer_item_number].badgeValue = value
+6
source

@Lepidopteron, . , :

let tabItems = self.tabBarController?.tabBar.items as NSArray!
    var selectedIndex = tabBarController!.selectedIndex //here 
    let tabItem = tabItems![selectedIndex] as! UITabBarItem
    tabItem.badgeValue = "2"

this post

0

Source: https://habr.com/ru/post/1016160/


All Articles