如何用swift设置标签栏徽章?

时间:2022-08-26 21:34:30

How to set the tab bar badge with swift ? for example when I get new message showing number 1 on the message icon ! Do I have to use the UITabBarItem.swift and write the code in it ! I'm not really sure how I can do it

如何用swift设置标签栏徽章?例如,当我收到消息图标上显示数字1的新消息时!我是否必须使用UITabBarItem.swift并在其中编写代码!我不确定我怎么能这样做

Thank you !

谢谢 !

3 个解决方案

#1


28  

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

如果您获得了对tabBarController的引用(例如,来自UIViewController),您可以执行以下操作:

Swift 3/4

斯威夫特3/4

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 a UITabBarController it would be:

从UITabBarController它将是:

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

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

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

and to delete the Badge:

并删除徽章:

tabItem.badgeValue = nil

#2


9  

The following line may help you to show badge in UITabBerItem

以下行可以帮助您在UITabBerItem中显示徽章

tabBarController?.tabBar.items?[your_desired_tabBer_item_number].badgeValue = value

#3


0  

Thanks to @Lepidopteron, instant solution for me. In addition, you can do it with the index of selected tab index:

感谢@Lepidopteron,我的即时解决方案。此外,您可以使用所选选项卡索引的索引来执行此操作:

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

Got the reference from this post

得到了这篇文章的参考

#1


28  

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

如果您获得了对tabBarController的引用(例如,来自UIViewController),您可以执行以下操作:

Swift 3/4

斯威夫特3/4

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 a UITabBarController it would be:

从UITabBarController它将是:

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

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

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

and to delete the Badge:

并删除徽章:

tabItem.badgeValue = nil

#2


9  

The following line may help you to show badge in UITabBerItem

以下行可以帮助您在UITabBerItem中显示徽章

tabBarController?.tabBar.items?[your_desired_tabBer_item_number].badgeValue = value

#3


0  

Thanks to @Lepidopteron, instant solution for me. In addition, you can do it with the index of selected tab index:

感谢@Lepidopteron,我的即时解决方案。此外,您可以使用所选选项卡索引的索引来执行此操作:

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

Got the reference from this post

得到了这篇文章的参考