0

I am using UIBarButtonItem-Badge. When created UIBarButtonItem in storyboard badge display fine. But when created UIBarButtonItem programmatically badge not displayed.

Following is my Code:

@property UIBarButtonItem *pendingRequestButton;

viewDidLoad method:

- (void)viewDidLoad {
    _pendingRequestButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"PendingRequestBarButtonItemImage_filled"] style:UIBarButtonItemStylePlain target:self  action:@selector(pendingRequestButtonAction:)];
    [self.navigationItem setLeftBarButtonItem:_pendingRequestButton animated:YES];
}

viewDidAppear method:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_pendingRequestButton setBadgeValue:[NSString stringWithFormat:@"%ld",[[[UserDefaultsManager sharedManager] objectForKey:@"panding_requests_count"] integerValue]]];
    [_pendingRequestButton setBadgeBGColor:[UIColor yellowColor]];
    [_pendingRequestButton setBadgeTextColor:[UIColor whiteColor]];
}

Can anyone help me to solve this problem.

Thanks in advance!

il3v
  • 472
  • 4
  • 15
Chirag Kothiya
  • 955
  • 5
  • 12
  • 28
  • 1
    Can you add the code for setBadgeValue:, setBadgeBGColor:, and setbadgeTextColor methods? – Iosif Aug 23 '18 at 12:24
  • [this lib](https://github.com/mikeMTOL/UIBarButtonItem-Badge) – Chirag Kothiya Aug 23 '18 at 12:45
  • It would have been nice if you'd have told us from the beginning that you are using that external library. Anyway, that's a library not being updated in the last four years, and the bar button items have had a massive rethinking not too long ago, so it *might* be just a compatibility issue. May I suggest you try and look for something more fresh? – il3v Aug 23 '18 at 14:00

3 Answers3

0

try this in objective c

self.tabBarController?.tabBarItem.badgeColor = UIColor.red self.tabBarController?.tabBarItem.badgeValue = "12"

0

I think I spot the issue, it seems you forget to add the bar button to the navigation item in viewDidLoad.

self.navigationItem.leftBarButtonItem = _pendingRequestButton;
Iosif
  • 353
  • 2
  • 15
  • I add this sorry I forgot to write in question. – Chirag Kothiya Aug 23 '18 at 13:02
  • 1
    I think there is a problem with the library it self. I had run the demo project and I end up in the same problem. It seems to work only for the case when you init with custom view, `[[UIBarButtonItem alloc] initWithCustomView:button]` – Iosif Aug 23 '18 at 13:34
  • There is in fact a problem with the library. See my answer. – il3v Aug 23 '18 at 14:08
0

It is a compatibility issue between UIBarButtonItem-Badge and iOS 11, as stated here

il3v
  • 472
  • 4
  • 15
  • I am use PPBadgeView: https://github.com/jkpang/PPBadgeView but same issue. – Chirag Kothiya Aug 23 '18 at 14:22
  • This is a different issue, being it a totally different library. Are you using the same code? – il3v Aug 23 '18 at 14:59
  • I am create UIBarButtonItem programmatically. when create in storyboard it work. – Chirag Kothiya Aug 24 '18 at 06:05
  • Then, since the issue is related to another library and the code is different, please mark this question as resolved and open a new one stating that you are using that other library and showing the code you are using. – il3v Aug 24 '18 at 07:17