How to add Badges on UIBarbutton item?

2019-01-06 08:47发布

Hi friends am new to iphone developing. Am struggle with add badge values on UIBarbutton item on right side. I have tried but i can't solve this problem. Can anyone help me.

Thanks in advance!

9条回答
何必那么认真
2楼-- · 2019-01-06 09:41

After searching too many solutions I found this best solution for Objective-C

Goto Following Link and download two files "UIBarButtonItem+Badge.h" and "UIBarButtonItem+Badge.m" and add to your project :

https://github.com/mikeMTOL/UIBarButtonItem-Badge

Then import in your class :

#import "UIBarButtonItem+Badge.h"

And write down following line to add badge :

self.navigationItem.rightBarButtonItem.badgeValue = @"1"; //your value 

Hope it will Work !!!

查看更多
Viruses.
3楼-- · 2019-01-06 09:47

I know this post is pretty old but with iOS7, MKNumberBadgeView's appearance does not really match the tab bar item badge design. I have found this other component which herits UIBarButtonItem and do the job very well :

https://github.com/TanguyAladenise/BBBadgeBarButtonItem

Hope this may help other iOS7 developers like me

查看更多
forever°为你锁心
4楼-- · 2019-01-06 09:51

Extension to add an UIActivityIndicatorView without replacing the UIBarButtonItem.

extension UIBarButtonItem {

func startLoading() {
    guard let view = self.value(forKey: "view") as? UIView else { return }
    let loading = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    loading.frame = view.bounds
    loading.startAnimating()
    view.addSubview(loading)
    view.bringSubview(toFront: loading)
    let buttonView = view.subviews.first
    buttonView?.alpha = 0.1
}

func stopLoading() {
    guard let view = self.value(forKey: "view") as? UIView else { return }
    let loading = view.subviews.filter({ $0 is UIActivityIndicatorView }).first
    loading?.removeFromSuperview()
    let buttonView = view.subviews.first
    buttonView?.alpha = 1.0
}

}

查看更多
登录 后发表回答