Badge value on “More” tab

懵懂的女人 提交于 2019-12-10 23:55:36

问题


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:[NSString 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.


回答1:


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.




回答2:


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...




回答3:


You can use for Swift 4:

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



来源:https://stackoverflow.com/questions/6520526/badge-value-on-more-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!