在Swift中更新未选中tabBarItem的徽章 [英] Update badge of non selected tabBarItem in Swift

查看:82
本文介绍了在Swift中更新未选中tabBarItem的徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有4个标签栏项目的导航控制器。每个人都有一个导航控制器。我希望能够在我获得推送通知时更改第4个标签栏徽章编号,无论在哪个视图或标签中,我都需要使用自动布局,因此我无法在应用程序委托中使用任何编程解决方案。我从单个视图模板开始了该项目。

I have a navigation controller with 4 tab bar items. Each one has a navigation controller inside. I want to be able to change the 4th tab bar badge number when I get push notification, no matter in what view or tab am I. I need to use the auto-layout so I can't use any programmatically solution inside the app delegate. I started the project from a single view template.

我试图转到所需的标签,更改标记值并返回,但当然它不起作用。
tabBarController似乎只有对当前标签栏项的引用。

I tried to go to the desired tab, change the badge value and come back but of course it didn't work. The tabBarController seems to have only references to the current tab bar item.

    var current = self.tabBarController?.selectedIndex
    self.tabBarController?.selectedIndex = 3
    self.navigationController?.tabBarItem.badgeValue = "34"
    self.tabBarController?.selectedIndex = current!


推荐答案

无需选择该索引来更新徽章值。获取一系列标签栏项目。要更新的索引处的选择项,然后将其设置为徽章值。见下面我为第四个标签栏项目做了。

No need to select that index to update badge value. Take an array of tab bar items. The select item at the index which you want to update and then set it badge value. See below I have done for 4th tab bar item.

Swift 4.0编辑:

let tabArray = self.tabBarController?.tabBar.items as NSArray!
let tabItem = tabArray?.object(at: 3) as! UITabBarItem
tabItem.badgeValue = "34"

这篇关于在Swift中更新未选中tabBarItem的徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆