【问题标题】:Failed to setBadgeValue in tab bar Objective C无法在标签栏目标 C 中设置BadgeValue
【发布时间】:2018-07-14 20:36:16
【问题描述】:

我想在收到新警报时在标签栏中设置徽章值。

我曾尝试将此代码[[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"]; 放入viewDidLoad,但没有成功。

这是我在 TabbarController 中的代码。

任何人都可以帮忙检查一下吗?非常感谢您的帮助

@interface TabbarController ()<UITabBarControllerDelegate>
@end
@implementation TabbarController

+ (void)initialize{

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = PFR11Font;
    attrs[NSForegroundColorAttributeName] = ThemeBlueColor;

    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = ThemeBlueColor;

    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    [UITabBar appearance].translucent = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];

    self.view.backgroundColor = [UIColor whiteColor];
    self.delegate = self;

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       ThemeBlueColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];

    [self setupChildVc:[[DCHandPickViewController alloc] init] title:@"Home" image:@"tabr_01_up" selectedImage:@"tabr_01_down"];

    [self setupChildVc:[[DCCommodityViewController alloc] init] title:@"Category" image:@"tabr_02_up" selectedImage:@"tabr_02_down"];

    [self setupChildVc:[[Map_ViewController alloc] init] title:@"Map" image:@"tabr_05_up" selectedImage:@"tabr_05_down"];

     [self setupChildVc:[[DCMyTrolleyViewController alloc] init] title:@"Cart" image:@"tabr_04_up" selectedImage:@"tabr_04_down"];

    [self setupChildVc:[[Chat_ViewController alloc] init] title:@"Chat" image:@"tabr_03_up" selectedImage:@"tabr_03_down"];

}

- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];

    NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if (!iPhoneX) return;

    for (UIView *view in self.tabBar.subviews) {
        if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
            CGRect frame = view.frame;
            frame.size.height = 48;
            view.frame = frame;
            NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:48];
            heightConstraint.priority = UILayoutPriorityDefaultHigh;

        }
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

}


#pragma mark - <UITabBarControllerDelegate>
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    [self tabBarButtonClick:[self getTabBarButton]];

}
- (UIControl *)getTabBarButton{
    NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];

    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            [tabBarButtons addObject:tabBarButton];
        }
    }
    UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];

    return tabBarButton;
}

- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {

            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.3;
            animation.calculationMode = kCAAnimationCubic;

            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}

@end

有什么想法吗?

【问题讨论】:

  • 调用时标签栏中是否有项目?例如self.tabBarController.tabBar.items 的值是多少
  • 嗨 Daij-Djan,我在上面编辑了我的代码。请看一看。谢谢
  • 马特说了什么。请接受变量并告诉我们我上面问的值^^很确定
  • 嗨 Daij-Djan,知道了,我输入了这段代码 [[self.tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"2"];在我的视图控制器页面中。谢谢帮助

标签: ios objective-c tabbar


【解决方案1】:

我已尝试将此代码 [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"]; 放入 viewDidLoad

如果该代码在标签栏控制器中,那么self.tabBarController 就是nil,并且代码什么也不做。你应该改用self.tabBar,就像在其他地方一样。

【讨论】:

  • 嗨,马特,我把它改成了 [[self.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];但仍然没有出现。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多