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

yshpjwxd  于 2023-08-02  发布在  Swift
关注(0)|答案(7)|浏览(146)

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

5uzkadbs

5uzkadbs1#

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

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

字符串
在UITabBarController中,它将是tabBar.items,而不是tabBarController?.tabBar.items
删除徽章:

  1. tabItem.badgeValue = nil

biswetbf

biswetbf2#

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

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

字符串

apeeds0o

apeeds0o3#

如果需要,也可以为标记值设置一个空字符串,以获得一个红色圆圈:

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

字符串


的数据

1tu0hz3e

1tu0hz3e4#

ViewDidAppear中设置badgeValue。否则,它可能不会从应用程序加载中出现。

  1. import UIKit
  2. class TabBarController: UITabBarController {
  3. override func viewDidAppear(_ animated: Bool) {
  4. super.viewDidAppear(animated)
  5. self.tabBar.items![2].badgeValue = "7"
  6. }
  7. }

字符串
没有安全检查,因为您通常可以确保TabBar具有n个选项卡。

7dl7o3gd

7dl7o3gd5#

我将@维克托代码放在扩展中,以使代码在视图中更小。

  1. import UIKit
  2. extension UITabBar {
  3. func addBadge(index:Int) {
  4. if let tabItems = self.items {
  5. let tabItem = tabItems[index]
  6. tabItem.badgeValue = "●"
  7. tabItem.badgeColor = .clear
  8. tabItem.setBadgeTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
  9. }
  10. }
  11. func removeBadge(index:Int) {
  12. if let tabItems = self.items {
  13. let tabItem = tabItems[index]
  14. tabItem.badgeValue = nil
  15. }
  16. }
  17. }
  18. Application:
  19. tabBarController?.tabBar.addBadge(index: 1)
  20. tabBarController?.tabBar.removeBadge(index: 1)

字符串

展开查看全部
neekobn8

neekobn86#

感谢@Lepidopteron,即时解决我.此外,您可以使用所选标签索引的索引来执行:

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

字符串
this帖子获得参考

cyej8jka

cyej8jka7#

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

字符串

展开查看全部

相关问题