UIBarButtonItem-Badge for iOS

主宰稳场 提交于 2019-12-07 17:38:48

问题


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

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