首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >标签栏目标C中的setBadgeValue失败

问标签栏目标C中的setBadgeValue失败
EN

Stack Overflow用户
提问于 2018-02-05 02:02:18
回答 1查看 90关注 0票数 0

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

我尝试过将这个代码[[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];放在viewDidLoad中,但没有成功。

这是我在TabbarController中的代码。

有人能帮我查一下吗?我们非常感谢你的帮助。

代码语言:javascript
复制
@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

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-05 03:32:56

我尝试将这个代码[[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];放在viewDidLoad中

如果该代码在选项卡条控制器中,那么self.tabBarController是nil,而代码什么也不做。你应该说self.tabBar,就像在其他地方一样。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48614935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档