“更多”标签上的徽章值

问题描述 投票:2回答:3

我正在制作一个iOS应用程序,它有一个tabBarController,有超过5个标签。因此,前四个可以直接点击,其余的都在MORE选项卡下。

如果在此MORE选项卡中隐藏了选项卡的任何徽章,我想在MORE选项卡上显示徽章。

我知道如何从这个question做到这一点。

但我的标签顺序是可配置的。有没有办法我可以配置MORE选项卡,如果我为其中的选项卡设置一个值,它只是放置badgeValue?

我在想这个:

- (void)updateBadgeValue:(NSInteger)count {
    int index = [self.tabBarController indexOfObject:self.tabBarItem];
    if (index > 4) { //Inside MORE tab
        [[[self.tabBarController moreTabBarController] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", count]];
    }
    //Also setting badge of self.tabbarItem so that it remains when it is brought to "hot tab items".
}

我正在寻找一个解决方案,这样我就不必为每个标签做到这一点。此外,如果用户更改了Tab键顺序,则badgeValue也应相应更新。

谢谢。

iphone objective-c uitabbar badge
3个回答
4
投票

试试这个:

- (void)updateBadgeValue:(NSInteger)count {
    int index = [self.tabBarController indexOfObject:self.tabBarItem];
    if (index > 4) {
        int moreTabCount = count + [[[[self.tabBarController moreTabBarController] tabBarItem] badgeValue] intValue];
        [[[self.tabBarController moreTabBarController] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", moreTabCount]];
    }
}

更新:您可以使用以下方式响应配置更改

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed

你的UITabBarController委托中的委托方法(应该是AppDelegate)。我们开始做吧:

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
    if(changed) {
        int count = 0;
        int i; for(i = 4; i < [viewControllers count]; i++)
            count += [[[[viewControllers objectAtIndex:i] tabBarItem] badgeValue] intValue];
        if(count > 0)
            [[self.tabBarController moreTabBarController] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", count]];
    }
}

我认为它会起作用。


1
投票

试试这个

[[[[[self tabBarController] tabBar] items] 
          objectAtIndex:4] setBadgeValue:[NSString stringWithFormat:@"%d",yourBadgeValue];

这里ObjectAtIndex用于你的Tab,其中0表示你的第一个标签等...


0
投票

你可以用于Swift 4:

let messagesCount:Int = 5 self.tabBarController?.moreNavigationController.tabBarItem.badgeValue =“\(messagesCount)”

© www.soinside.com 2019 - 2024. All rights reserved.