问题
I am using this control for showing badge on barbuttonitem. It works fine for the first controller or root controller in navigation controller stack. While pushing to another controller, I tried to show badge and I cannot see any effect there.
回答1:
You should first time set badgeValue in viewDidLayoutSubviews
. In this case it'll appear and don't blink, like if you set it in viewDidAppear
:
- (void)viewDidLayoutSubviews {
barButton.badgeValue = @"5";
}
回答2:
Setting navigationItem
in viewDidLayoutSubviews
is not recommended because it is not semantic.
The key to your problem is that the badge view is added to [UIBarButtonItem valueForKey:@"view"]
, but when UIBarButtonItem
is initialized, valueForKey:@"view"
doesn't exist yet, so adding a badge fails.
I suggest you try QMUIBadge, it can solve your problem.
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] init];
rightItem.qmui_badgeInteger = 1;// number
rightItem.qmui_badgeString = @"99+";// or string
// setRightBarButtonItem any time
self.navigationItem.rightBarButtonItem = rightItem;
来源:https://stackoverflow.com/questions/34611639/uibarbuttonitem-badge-for-ios