是否可以自定义UITabBarItem徽章?
下面的问题与我的类似。是否可以自定义UITabBarItem徽章?
How to use a custom UIImage as an UITabBarItem Badge?
是否有可能使用背景图片,而不是绘制它自己的? 我认为这很好,因为我的应用只会使用1位的badgeValue。
如果它不是真的可能,我想知道我们是否可以改变徽章的颜色。 下面问题中的答案并不真正有帮助。
嗯......改变内置徽章的背景似乎并不可能给我。但是可能的代替是:
子类TabBarController,构建一个自定义视图放置在NIB中,将它添加到标签栏中位置的视图层次结构中,您希望它的位置。
在自定义视图中,您可以将图像设置为背景,并在该背景上设置标签,然后显示数字值,然后可以通过代码进行更改。
然后,您需要计算出自定义视图的水平和垂直位置,以便将视图放置在选项卡栏内。
希望这有助于一点点。 塞巴斯蒂安
这是你最好的选择。在文件范围内添加此扩展名,然后您可以自定义标签。请在任何根视图控制器中拨打self.tabBarController!.setBadges([1,0,2])
。
要明确这是一个带有三个项目的标签栏,徽章值从左到右。
如果要添加图像,而不是仅仅改变addBadge
方法
extension UITabBarController {
func setBadges(badgeValues:[Int]){
var labelExistsForIndex = [Bool]()
for value in badgeValues {
labelExistsForIndex.append(false)
}
for view in self.tabBar.subviews {
if view.isKindOfClass(PGTabBadge) {
let badgeView = view as! PGTabBadge
let index = badgeView.tag
if badgeValues[index]==0 {
badgeView.removeFromSuperview()
}
labelExistsForIndex[index]=true
badgeView.text = String(badgeValues[index])
}
}
for var i=0;i<labelExistsForIndex.count;i++ {
if labelExistsForIndex[i] == false {
if badgeValues[i] > 0 {
addBadge(i, value: badgeValues[i], color:UIColor(red: 4/255, green: 110/255, blue: 188/255, alpha: 1), font: UIFont(name: "Helvetica-Light", size: 11)!)
}
}
}
}
func addBadge(index:Int,value:Int, color:UIColor, font:UIFont){
let itemPosition = CGFloat(index+1)
let itemWidth:CGFloat = tabBar.frame.width/CGFloat(tabBar.items!.count)
let bgColor = color
let xOffset:CGFloat = 12
let yOffset:CGFloat = -9
var badgeView = PGTabBadge()
badgeView.frame.size=CGSizeMake(17, 17)
badgeView.center=CGPointMake((itemWidth * itemPosition)-(itemWidth/2)+xOffset, 20+yOffset)
badgeView.layer.cornerRadius=badgeView.bounds.width/2
badgeView.clipsToBounds=true
badgeView.textColor=UIColor.whiteColor()
badgeView.textAlignment = .Center
badgeView.font = font
badgeView.text = String(value)
badgeView.backgroundColor = bgColor
badgeView.tag=index
tabBar.addSubview(badgeView)
}
}
class PGTabBadge: UILabel {
}
这个App Store安全吗? – 2015-09-29 16:05:07
绝对的,标签栏只是一个视图,所以添加子视图是完全合法的。 – TimWhiting 2015-10-02 22:04:58
您可以使用更强大的解决方案@UITabbarItem-CustomBadge。
演示
简单的两行代码可以让你去
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//supplying the animation parameter
[UITabBarItem setDefaultAnimationProvider:[[DefaultTabbarBadgeAnimation alloc] init]];
[UITabBarItem setDefaultConfigurationProvider:[[DefaultSystemLikeBadgeConfiguration alloc] init]];
//rest of your code goes following...
return YES;
}
请留下评论的理由,如果有人会离开-1 – 2016-11-22 15:45:00
通过我自己的github存储库的方式,我应该添加完整的6个代码文件?你没事吧? @Zoleas – 2016-11-22 16:49:06
感谢您对这个想法。我会试试这个。但我有一个后续问题。这个徽章定制是否正确,如果这个应用程序将被提交给AppStore? – 2013-04-29 08:46:16
欢迎您...如果您满意,请热情接受答案。 嗯....这是没有问题的,因为如果你不喜欢它,就不需要使用苹果提供的徽章。如果您尝试修改由Apple提供的私人课程,您只会遇到麻烦。 – sesc360 2013-04-29 09:21:23