如何在iOS Swift中将绿色徽章设置为特定的tabBarItem?

idfiyjo8  于 2023-05-05  发布在  Swift
关注(0)|答案(1)|浏览(195)

目前,我正试图设置绿色这种方式,但最终结果总是一个红色徽章。

xxxx.tabBarItem.badgeColor = .clear
        xxxx.tabBarItem.badge value = "●"

        
        let attribute : [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.green]

        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .disabled)
        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .selected)
        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .highlighted)
        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .normal)

我想要什么?

但代码结果为:

fzwojiic

fzwojiic1#

会成功的使用tabBar.items代替tabBarItem.badgeValue

self.tabBarController?.tabBar.items?[0].badgeColor = .clear

 self.tabBarController?.tabBar.items?[0].badgeValue = "●"

 let attribute : [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.green]

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .disabled)

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .selected)

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .highlighted)

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .normal)

相关问题