UIBarButtonItem-徽章:徽章不会显示

时间:2018-08-23 12:00:19

标签: ios objective-c uibarbuttonitem badge

我正在使用UIBarButtonItem-Badge。在情节提要标记中创建UIBarButtonItem时,徽章显示会很好。但是创建UIBarButtonItem时,以编程方式不会显示徽章。

以下是我的代码:

@property UIBarButtonItem *pendingRequestButton;

viewDidLoad方法:

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

viewDidAppear方法:

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

谁能帮我解决这个问题。

谢谢!

3 个答案:

答案 0 :(得分:0)

在目标c中尝试

self.tabBarController?.tabBarItem.badgeColor = UIColor.red         self.tabBarController?.tabBarItem.badgeValue =“ 12”

答案 1 :(得分:0)

我想我发现了这个问题,看来您忘记了将bar按钮添加到viewDidLoad中的导航项中。

self.navigationItem.leftBarButtonItem = _pendingRequestButton;

答案 2 :(得分:0)

这是UIBarButtonItem-Badge与iOS 11之间的兼容性问题,here