开发者

Badge value on "More" tab

I am making an iOS app which has a tabBarController, with more than 5 tabs. Thus, first four are directly clickable and the rest come under MORE tab.

I want to show a badge on the MORE tab if there is any badge for the tabs that are hidden inside this MORE tab.

I know about how to do that from this question.

But the order of my tabs is configurable. Is there a way I can configure the MORE tab such it just puts the badgeValue if I set a value for a tab inside it?

I am thinking of this:

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

I am looking for a solution such that I dont have to do that for every tab. Also, if the tab order is changed by the user, the badgeValue should also update accordingly.

Thanks.


Try using this:

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

UPDATE: You can respond to configuration changes by using

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

delegate method in your UITabBarController's delegate (it should be AppDelegate). Let's do it:

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

I think that it will work.


Try using this

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

Here ObjectAtIndex is for your Tab where 0 represents your first tab etc...


You can use for Swift 4:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜