How to set a badge value on a UITabBarController item

本文介绍如何在iOS应用中为UITabBar项设置Badge值。Badge值是显示在Tab栏图标上方的小红标记,可以用来提示用户有关更新或通知的信息。文章提供了设置不同样式Badge值的方法,并解释了如何通过UIViewController或UINavigationController来访问并修改这些值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述
Each view controller has access to a UITabBarItem. This is only displayed if the current view controller is part of a UITabBarController. A UITabBarItem has a badge value which is that little red mark above icon and name to the tab bar item itself (as shown in the screenshot above: LIVE is currently the badgeValue of the tab bar item).

Here’s how you can set the badge value:

// set the tab bar value (any NSString will work):
self.tabBarItem.badgeValue = @”LIVE”;

// make it disappear again
self.tabBarItem.badgeValue = nil;

// set it to a little red circle (no value, just an empty string)
self.tabBarItem.badgeValue = @”“;
This approach will work if you have direct access to the class in the view controller. More commonly though you’ll have your own class embedded in a UINavigationController, in which case its tab bar item (and title) is shown on that tab.

In which case you can access the tab bar item like this:

// set badge value on your own nav controller
self.navigationController.tabBarItem.badgeValue = @”LIVE”;
https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarItem_Class/Reference/Reference.html

copywrite :http://pinkstone.co.uk/how-to-set-a-badge-value-on-a-uitabbarcontroller-item/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值