如何使用Swift设置选项卡栏标记?

60

如何使用 Swift 设置选项卡栏徽章?例如,当我收到新消息时,在消息图标上显示数字 1!我需要使用 UITabBarItem.swift 并在其中编写代码吗?我不太确定该如何实现。

谢谢!

7个回答

149

如果您从UIViewController获取了对tabBarController的引用,您可以执行以下操作:

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

从 UITabBarController 中,使用 tabBar.items 而不是 tabBarController?.tabBar.items

要删除徽章:

tabItem.badgeValue = nil

1
你能解释一下这段代码吗?tabArray是什么意思? - Faris
1
你从哪里得到了tabArray? - Joe Sene
1
我应该在哪里使用这段代码?是在我的TabBarController内还是对应于选项卡项目的ViewController内,例如我的通知选项卡? - Joe Sene
请将以下与编程有关的内容从英语翻译成中文。请仅返回翻译后的文本:tabItem.badgeValue = self.tableView.dataSource.numberOfRowsInSection(self.tableView)。 - Lepidopteron
它起作用了,虽然我可以让它在应用程序启动时立即显示,但只有当我点击该项时才会显示徽章。 - Joe Sene
显示剩余3条评论

19
以下代码可能会帮助您在UITabBarItem中显示徽章:
tabBarController?.tabBar.items?[your_desired_tabBer_item_number].badgeValue = value

3
请在您的代码中添加解释。仅提供代码的答案被认为是低质量的,通常会被下投票甚至可能被删除。请参见[答案]。 - Bugs
2
  1. tabBarController? - 解包,因为我们不确定它是否存在于我们的UIVewController中。
  2. .tabBar.items? - 询问UITabBarController中tabBar中的项目数组,也要解包,因为我们不知道这个数组是否初始化了至少一个UITabBarItem。
  3. .[UITabBarItem index in array] - 数组中UITabBarItem的索引。
  4. .badgeValue - 您想要查看的UITabBarItem的徽章字符串值。
- Mykyta Savchuk

9

如果需要,可以将徽章值设置为空字符串以获得红色圆形:

tabBarController?.tabBar.items?.last?.badgeValue = ""

enter image description here


你在 iOS 15 上尝试过了吗? - famfamfam
@famfamfam 它也适用于iOS 15。 - Fengson
不,它在iOS 15上无法工作。 - famfamfam
1
我在 iOS 15.2 上测试过了,它可以正常工作。 - Ahmed Shendy

8

ViewDidAppear 中设置 badgeValue。否则,在应用加载时可能不会显示。

import UIKit

class TabBarController: UITabBarController {

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    self.tabBar.items![2].badgeValue = "7"
}

}

由于您通常可以确定您拥有具有n个选项卡的TabBar,因此没有安全检查。


6

我采用了 @Victor 的代码,并将其放入扩展中以使视图中的代码更小。

import UIKit

extension UITabBar {
    func addBadge(index:Int) {
        if let tabItems = self.items {
            let tabItem = tabItems[index]
            tabItem.badgeValue = "●"
            tabItem.badgeColor = .clear
            tabItem.setBadgeTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
        }
    }
    func removeBadge(index:Int) {
        if let tabItems = self.items {
            let tabItem = tabItems[index]
            tabItem.badgeValue = nil
        }
    }
    
 }


Application: 

 tabBarController?.tabBar.addBadge(index: 1)

 tabBarController?.tabBar.removeBadge(index: 1)




1
感谢 @Lepidopteron,对我来说是一个即时的解决方案。 此外,您可以使用所选选项卡索引来完成它:
let tabItems = self.tabBarController?.tabBar.items as NSArray!
    var selectedIndex = tabBarController!.selectedIndex //here 
    let tabItem = tabItems![selectedIndex] as! UITabBarItem
    tabItem.badgeValue = "2"

this帖子得到了参考资料。

0
func showHideInnerBagde(value:String,indexValue:Int) {
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let sceneDelegate = windowScene.delegate as? SceneDelegate,
           let navController = sceneDelegate.window?.rootViewController as? UINavigationController,
           let visibleController = navController.visibleViewController,
           let tabbarController = visibleController as? UITabBarController,
           tabbarController.tabBar.items?.indices.contains(2) ?? false {
            
            let tabIndex = indexValue
            tabbarController.tabBar.items?[tabIndex].badgeValue = value
            tabbarController.tabBar.items?[tabIndex].badgeColor = UIColor.clear
            
            let attributes: [NSAttributedString.Key: Any] = [
                NSAttributedString.Key.foregroundColor: UIColor.red,
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12) // Set your desired font
            ]
            tabbarController.tabBar.items?[tabIndex].setBadgeTextAttributes(attributes, for: .normal)
        } else {
            // Handle the case when the conditions are not met or the tab at index 2 does not exist.
            print("Unable to set badge value and text attributes.")
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接