Awesome
BadgeHub
A way to quickly add a notification badge icon to any view.
<img src="https://user-images.githubusercontent.com/20956124/52379966-080a5f00-2a92-11e9-8c85-9c34fabd4641.png">
Demo/Example
For demo:
$ 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
Requirements
- iOS 10.0 or later
- Swift 5+
- Xcode 10+
Installation
CocoaPods
BadgeHub is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'BadgeHub'
Manual Installation
Just drag the BadgeHub.swift
files into your project.
Usage
Initialization.
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.
hub.increment()
Increase count by some int value.
hub.increment(by: Int)
Decrease count value by 1.
hub.decrement()
Decrease count by some int value.
hub.decrement(by: Int)
Set count to static integer value.
hub.setCount(newCount: Int)
Get value of current count on badge.
hub.getCurrentCount() // returns Int value of current count.
Combine actions
hub.increment()
hub.pop()
hub.blink()
Don't forget to import BadgeHub
Customization
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.
hub.hideCount()
Show count again on the badge.
hub.showCount()
Hide the badge from your view.
hub.hide()
Show again the badge. Badge will staye hidden even after calling this method, if current count on badge is <= 0.
hub.show()
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).
hub.setMaxCount(to: Int)
Set the font of the count label.
hub.setCountLabelFont(_ font: UIFont?)
Get the current font set on the count label.
hub.getCountLabelFont()
Set alpha to badge.
hub.setAlpha(alpha: CGFloat)
Animations
Pop out and pop in the badge.
hub.pop()
Make badge blinking.
hub.blink()
Animation that jumps similar to macOS dock icons.
hub.bump()
TROUBLESHOOTING
Notification isn't showing up!
- If the hub value is < 1, the circle hides. Try calling
increment()
. - Make sure the view you set the hub to is visible (i.e. did you call
self.view.addSubview(yourView)
?). - Make sure you didn't call
hideCount()
anywhere. CallshowCount()
to counter this.
Badge is not hiding even after setting value to 0
- Make sure you are setting zero count on correct BadgeHub instance.
- Try calling
checkZero()
method after setting count to 0. - Varify if current count is <= 0 by calling
getCurrentCount()
method. - Keep in mind that
hideCount()
method is for hiding just count, not the badge. To hide the badge, simply callhide()
.
It isn't incrementing / decrementing properly!
- Any count < 1 doesn't show up. If you need help customizing this, reach out to me!
The circle is in a weird place
- If you want to resize the circle, use
scaleCircleSize(by scale: CGFloat)
. 0.5 will give you half the size, 2 will give you double. - If the circle is just a few pixels off, use
moveCircleBy(x: CGFloat, y: CGFloat)
. This shifts the circle by the number of pixels given. - If you want to manually set the circle, call
setCircleAtFrame(_ frame: CGRect)
and give it your own CGRect.
Something else isn't working properly
- Use GitHub's issue reporter to submit a new issue.
- If you think you fix that, feel free to open a pull request fixing the same.
- Shoot me an email at jogendra.iitbhu@gmail.com.
Author
<table> <tr> <td> <img src="https://avatars2.githubusercontent.com/u/20956124?s=400&u=01fab3fc9bb3d2ee799e314d3fe23c54d1deeb07&v=4" width="180"/>Jogendra Kumar
<p align="center"> <a href = "https://github.com/jogendra"><img src = "http://www.iconninja.com/files/241/825/211/round-collaboration-social-github-code-circle-network-icon.svg" width="36" height = "36"/></a> <a href = "https://twitter.com/jogendrafx"><img src = "https://www.shareicon.net/download/2016/07/06/107115_media.svg" width="36" height="36"/></a> <a href = "https://www.linkedin.com/in/jogendrasingh24/"><img src = "http://www.iconninja.com/files/863/607/751/network-linkedin-social-connection-circular-circle-media-icon.svg" width="36" height="36"/></a> </p> </td> </tr> </table>License
BadgeHub is available under the MIT license. See the LICENSE file for more info.