【问题标题】:How to use a custom UIImage as an UITabBarItem Badge?如何使用自定义 UIImage 作为 UITabBarItem 徽章?
【发布时间】:2024-01-15 00:03:01
【问题描述】:

如何添加自定义 UIImage 作为 UITabBarItem 的徽章?
即设置UITabbarItem的徽章值时会显示。

【问题讨论】:

    标签: iphone ios uitabbar uitabbaritem


    【解决方案1】:

    考虑继承 UITabBarItem 并自己绘制它

    【讨论】:

    • UITabBarItem 甚至没有从 UIView 继承。你能澄清你的建议吗?
    【解决方案2】:

    Sascha Paulus 写了一个很棒的类来做到这一点:

    CustomBadge2.0

    您当然可以将它与子类化 UITabBarItem 结合使用。

    【讨论】:

      【解决方案3】:

      您可以为 CustomBadge 使用以下自定义类:

      https://github.com/ckteebe/CustomBadge/tree/master/Classes

      希望对你有帮助。

      【讨论】:

        【解决方案4】:

        可以看一下here。这里自定义项目添加为UILabel。考虑在此处使用您自己的选择。

        演示

        只需要两行代码,就能搞定

        - (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;
        }
        

        【讨论】: