如何使用swift设置标签栏徽章?例如,当我收到新消息时,消息图标上显示数字1!我必须使用UITabBarItem.swift并在其中编写代码吗!我真的不确定我怎么能做到这一点谢谢你,谢谢
5uzkadbs1#
如果您获得了tabBarController的引用(例如从UIViewController),您可以执行以下操作:
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"}
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删除徽章:
tabBar.items
tabBarController?.tabBar.items
tabItem.badgeValue = nil
型
biswetbf2#
下面的行可以帮助您在UITabBerItem中显示徽章
tabBarController?.tabBar.items?[your_desired_tabBer_item_number].badgeValue = value
字符串
apeeds0o3#
如果需要,也可以为标记值设置一个空字符串,以获得一个红色圆圈:
tabBarController?.tabBar.items?.last?.badgeValue = ""
的数据
1tu0hz3e4#
在ViewDidAppear中设置badgeValue。否则,它可能不会从应用程序加载中出现。
ViewDidAppear
badgeValue
import UIKitclass TabBarController: UITabBarController {override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) self.tabBar.items![2].badgeValue = "7"}}
import UIKit
class TabBarController: UITabBarController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tabBar.items![2].badgeValue = "7"
字符串没有安全检查,因为您通常可以确保TabBar具有n个选项卡。
TabBar
7dl7o3gd5#
我将@维克托代码放在扩展中,以使代码在视图中更小。
import UIKitextension 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)
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) {
Application:
tabBarController?.tabBar.addBadge(index: 1)
tabBarController?.tabBar.removeBadge(index: 1)
neekobn86#
感谢@Lepidopteron,即时解决我.此外,您可以使用所选标签索引的索引来执行:
let tabItems = self.tabBarController?.tabBar.items as NSArray! var selectedIndex = tabBarController!.selectedIndex //here let tabItem = tabItems![selectedIndex] as! UITabBarItem tabItem.badgeValue = "2"
let tabItems = self.tabBarController?.tabBar.items as NSArray!
var selectedIndex = tabBarController!.selectedIndex //here
let tabItem = tabItems![selectedIndex] as! UITabBarItem
tabItem.badgeValue = "2"
字符串从this帖子获得参考
cyej8jka7#
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.") } }
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.")
7条答案
按热度按时间5uzkadbs1#
如果您获得了tabBarController的引用(例如从UIViewController),您可以执行以下操作:
字符串
在UITabBarController中,它将是
tabBar.items
,而不是tabBarController?.tabBar.items
删除徽章:
型
biswetbf2#
下面的行可以帮助您在UITabBerItem中显示徽章
字符串
apeeds0o3#
如果需要,也可以为标记值设置一个空字符串,以获得一个红色圆圈:
字符串
的数据
1tu0hz3e4#
在
ViewDidAppear
中设置badgeValue
。否则,它可能不会从应用程序加载中出现。字符串
没有安全检查,因为您通常可以确保
TabBar
具有n个选项卡。7dl7o3gd5#
我将@维克托代码放在扩展中,以使代码在视图中更小。
字符串
neekobn86#
感谢@Lepidopteron,即时解决我.此外,您可以使用所选标签索引的索引来执行:
字符串
从this帖子获得参考
cyej8jka7#
字符串