A way to quickly add a notification badge icon to any view. Make any view of a full-fledged animated notification center.

Overview

BadgeHub

A way to quickly add a notification badge icon to any view.

CI Status Version License Platform Buy me a Coffee

Blink Bump Pop Custom setCircle showCount mix hideCount

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

mix

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.

setCircleColor

hub.setCircleColor(_ circleColor: UIColor?, label labelColor: UIColor?)

Change the border color and border width of the circle

Custom

hub.setCircleBorderColor(_ color: UIColor?, borderWidth width: CGFloat)

Set the frame of the notification badge circle relative to the view.

setCircle

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.

hideCount

hub.hideCount()

Show count again on the badge.

showCount

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.

Pop

hub.pop()

Make badge blinking.

Blink

hub.blink()

Animation that jumps similar to macOS dock icons.

Bump

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. Call showCount() 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 call hide().

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 [email protected].

Author

Jogendra Kumar

BUY ME A COFFEE?

If you loved my work, Buy me a Coffee here:

Buy Me A Coffee

License

BadgeHub is available under the MIT license. See the LICENSE file for more info.

Comments
  • Badge hiding behind view.

    Badge hiding behind view.

    I am trying to append badgehub onto a UIImageView. The badge is appearing however when it reaches the corner; it stays behind the imageview or view and just hides away.

    I tried setting the z-index however that doesn't seem to be affecting anything. Anyone else having this issue? Or found a solution. Cheers

    let hub = BadgeHub(view: cell.contentView) // Initially count set to 0
    hub.hubView?.layer.zPosition = 10000
    hub.increment()
    
    good first issue Hacktoberfest hacktoberfest-accepted 
    opened by Harryjeffs 6
  • Fixing implied internals

    Fixing implied internals

    Fixes the following:

    • #6 (Initializer not specifically marked public)
    • Several methods that were implicitly interpreted as internal

    Also bumps podspec to 0.1.1

    Note to @jogendra: anything not explicitly marked public or private is interpreted by the compiler as internal :)

    opened by MatrixSenpai 4
  • SetCount to 0 and checkZero not removing red dot

    SetCount to 0 and checkZero not removing red dot

    When setting my count to 0 the red dot stays as 1 and does not disappear.

    I have tried to call checkZero( ), decrement the count from one to zero instead of set to zero, and even refresh the views but nothing has worked.

    Is there a way to remove the hub from the view it is attached to? I looked around for removing subviews from views but I could not find anything.

    badgehub screen shot.pdf

    good first issue Hacktoberfest hacktoberfest-accepted 
    opened by JakeEllis 1
  • maxCount is not taken into account when calling the increment method

    maxCount is not taken into account when calling the increment method

    Hello everybody!

    I've found out that maxCount is taken into account only when we call the setCount method, unfortunately, this constant doesn't apply for calls of increment. Example:

    hub.setMaxCount(99)
    hub.increment(by: 150)
    // count == 150
    

    Compare their implementations to each other. increment:

        /// Increases count by 1
        public func increment() {
            increment(by: 1)
        }
        
        /// Increases count by amount.
        /// - Parameter amount: Increment count.
        public func increment(by amount: Int) {
            count += amount
        }
    

    setCount:

        /// Set the count yourself.
        /// - Parameter newCount: New count to be set to badge.
        public func setCount(_ newCount: Int) {
            self.count = newCount
            let labelText = count > maxCount ? "\(maxCount)+" : "\(count)"
            countLabel?.text = labelText
            checkZero()
        }
    
    opened by oleg-samoylov-ext-orion 0
  • Hide Count hiding wrong object?

    Hide Count hiding wrong object?

    Shouldn't Hide Count hide the label and not the badge? (it currently hides the badge and leaves the number)

    /// Hide the count (Blank Badge). /// Remember this only hide count, /// and not the red dot. public func hideCount() { countLabel?.isHidden = true }

    opened by justdan0227 0
  • UITableViewCell

    UITableViewCell

    how to you set the badge to be upper left corner in the text of a UITableViewCell.textLabel ? I tried setCircleAtFrame and it shows up left but cut off and when I scroll then it shows up left and a second one on the right? With dequeue do I have to do something to use it in a UITableViewCell?

    opened by justdan0227 2
  • init?(barButtonItem: UIBarButtonItem) create nil BadgeHub

    init?(barButtonItem: UIBarButtonItem) create nil BadgeHub

    When the barButtonItem create by storyboard, init?(barButtonItem: UIBarButtonItem) create not nil BadgeHub.

    But when the barButtonItem create by code in viewDidLoad function, init?(barButtonItem: UIBarButtonItem) create a nil BadgeHub, but barButtonItem is not nil.

        let hub = BadgeHub(barButtonItem: testItem)
        hub?.increment()
        
        let leftItem = UIBarButtonItem(title: "Badge", style: .plain, target: self, action: nil)
        navigationItem.leftBarButtonItem = leftItem
        
        let itemHub = BadgeHub(barButtonItem: leftItem)
        itemHub?.increment(by: 10)
    

    hub create by Storyboard's UIBarButtonItem ,itemHub create by code UIBarButtonItem: leftItem

    hub is not nil, itemHub is nil

    opened by pekanchuan 2
Releases(1.0.0)
Owner
Jogendra
iOS / Backend Engineer | Aviation | Go | Swift | GSoC '18 | IIT BHU'20
Jogendra
Twinkle is a Swift and easy way to make any UIView in your iOS or tvOS app twinkle.

Twinkle ✨ Twinkle is a Swift and easy way to make any UIView in your iOS or tvOS app twinkle. This library creates several CAEmitterLayers and animate

patrick piemonte 600 Nov 24, 2022
iOS 11 Control Center Slider

SectionedSlider Control Center Slider Requirements Installation Usage License Requirements iOS 8.0+ Swift 3.0+ Xcode 8.0+ Installation CocoaPods Cocoa

Leonardo Cardoso 361 Dec 3, 2022
Elissa displays a notification on top of a UITabBarItem or any UIView anchor view to reveal additional information.

Elissa Attach a local notification to any UIView to reveal additional user guidance. Usage Example Per default, Elissa will try to align to the center

null 169 Aug 14, 2022
Show progress in your app's Dock icon

DockProgress Show progress in your app's Dock icon This package is used in production by the Gifski app. You might also like some of my other apps. Re

Sindre Sorhus 958 Jan 2, 2023
NotSwiftUI is designed to help you create UI components quickly and efficiently with code!

NotSwiftUI NotSwiftUI is designed to help you create UI components quickly and efficiently with code! Capitalizing on the idea that most of the UI ele

Jonathan G. 50 Dec 20, 2022
Full configurable spreadsheet view user interfaces for iOS applications. With this framework, you can easily create complex layouts like schedule, gantt chart or timetable as if you are using Excel.

kishikawakatsumi/SpreadsheetView has moved! It is being actively maintained at bannzai/SpreadsheetView. This fork was created when the project was mov

Kishikawa Katsumi 34 Sep 26, 2022
Cool Animated music indicator view written in Swift

Cool Animated music indicator view written in Swift. ESTMusicIndicator is an implementation of NAKPlaybackIndicatorView in Swift for iOS 8. 本人著作的书籍《La

Aufree 465 Nov 28, 2022
Beautiful animated placeholders for showing loading of data

KALoader Create breautiful animated placeholders for showing loading of data. You can change colors like you want. Swift 4 compatible. Usage To add an

Kirill Avery 105 May 2, 2022
⚙ Add a preferences window to your macOS app in minutes

Preferences Add a preferences window to your macOS app in minutes Just pass in some view controllers and this package will take care of the rest. Requ

Sindre Sorhus 1.2k Jan 6, 2023
Add the Notch on the menubar like the new MacBook Pro.

iNotch Add the Notch on the menubar like the new MacBook Pro. Installation This app works on macOS 11.0 or later. Download iNotch.zip from releases pa

Takuto NAKAMURA (Kyome) 8 Apr 3, 2022
Newly is a drop in solution to add Twitter/Facebook/Linkedin style, new updates/tweets/posts available button

Newly is a drop in solution to add Twitter/Facebook/Linkedin style, new updates/tweets/posts available button. It can be used to notify user about new content availability and can other actions can be triggers using its delegate method.

Dhiraj Rajendra Jadhao 197 Sep 22, 2022
Easily add drop shadows, borders, and round corners to a UIView.

Easily add drop shadows, borders, rounded corners to a UIView. Installation CocoaPods Add the follwing to your Podfile: pod 'Shades' Usage Storyboard

Aaron Sutton 14 Jun 20, 2020
Make ComposableArchitecture work with UIKit

ComposableUIKit The ComposableArchitecture (TCA) library provides a way of structuring Swift code with the Redux-pattern. It is highly optimized for S

Manuel Maly 11 Nov 5, 2022
A custom stretchable header view for UIScrollView or any its subclasses with UIActivityIndicatorView and iPhone X safe area support for content reloading. Built for iOS 10 and later.

Arale A custom stretchable header view for UIScrollView or any its subclasses with UIActivityIndicatorView support for reloading your content. Built f

Putra Z. 43 Feb 4, 2022
List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model and doesn't depend on UI framework

SwiftListTreeDataSource List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model, so can

Dzmitry Antonenka 26 Nov 26, 2022
AGCircularPicker is helpful component for creating a controller aimed to manage any calculated parameter

We are pleased to offer you our new free lightweight plugin named AGCircularPicker. AGCircularPicker is helpful for creating a controller aimed to man

Agilie Team 617 Dec 19, 2022
Create SwiftUI Views with any data

Create SwiftUI Views with any data

Zach Eriksen 20 Jun 27, 2022
📹 Framework to Play a Video in the Background of any UIView

SwiftVideoBackground is an easy to use Swift framework that provides the ability to play a video on any UIView. This provides a beautiful UI for login

Wilson Ding 334 Jan 7, 2023
A better way to present a SFSafariViewController or start a ASWebAuthenticationSession in SwiftUI.

BetterSafariView A better way to present a SFSafariViewController or start a ASWebAuthenticationSession in SwiftUI. Contents Motivation Requirements U

Dongkyu Kim 392 Dec 31, 2022