A way to quickly add a notification badge icon to any view.
$ pod try BadgeHub
To run the example project, clone the repo, and run
pod install from the Example directory first.
$ cd Example
$ pod install
If you don't have CocoaPods installed, grab it with
[sudo] gem install cocoapods.
$ open BadgeHub.xcworkspace
BadgeHub is available through CocoaPods. To install it, simply add the following line to your Podfile:
Just drag the
BadgeHub.swift files into your project.
let hub = BadgeHub(view: yourView) // Initially count set to 0
Initializer for setting badge to bar button items.
let hub = BadgeHub(barButtonItem: UIBarButtonItem)
Increase count value by 1.
Increase count by some int value.
Decrease count value by 1.
Decrease count by some int value.
Set count to static integer value.
Get value of current count on badge.
hub.getCurrentCount() // returns Int value of current count.
hub.increment() hub.pop() hub.blink()
Don't forget to
Change the color of the notification circle, also the text color of count label.
hub.setCircleColor(_ circleColor: UIColor?, label labelColor: UIColor?)
Change the border color and border width of the circle
hub.setCircleBorderColor(_ color: UIColor?, borderWidth width: CGFloat)
Set the frame of the notification badge circle relative to the view.
hub.setCircleAtFrame(_ frame: CGRect)
Move the circle (left/right or up/down).
hub.moveCircleBy(x: CGFloat, y: CGFloat)
Changes the size of the circle. setting a scale of 1 has no effect.
hub.scaleCircleSize(by scale: CGFloat)
Hide the count (Blank Badge). Keep in mind that this method is for hiding just count, not the badge.
Show count again on the badge.
Hide the badge from your view.
Show again the badge. Badge will staye hidden even after calling this method, if current count on badge is <= 0.
Set max count which can be displayed. This method can be used to restrict the maximum count can be set on the badge. Default value for max count is
100000. If you increase current count to more than max count, badge will display it like
500+ (if max count is 500).
Set the font of the count label.
hub.setCountLabelFont(_ font: UIFont?)
Get the current font set on the count label.
Set alpha to badge.
Pop out and pop in the badge.
Make badge blinking.
Animation that jumps similar to macOS dock icons.
Notification isn't showing up!
showCount()to counter this.
Badge is not hiding even after setting value to 0
checkZero()method after setting count to 0.
hideCount()method is for hiding just count, not the badge. To hide the badge, simply call
It isn't incrementing / decrementing properly!
The circle is in a weird place
scaleCircleSize(by scale: CGFloat). 0.5 will give you half the size, 2 will give you double.
moveCircleBy(x: CGFloat, y: CGFloat). This shifts the circle by the number of pixels given.
setCircleAtFrame(_ frame: CGRect)and give it your own CGRect.
Something else isn't working properly
If you loved my work, Buy me a Coffee here:
BadgeHub is available under the MIT license. See the LICENSE file for more info.