Giter VIP home page Giter VIP logo

jogendra / badgehub Goto Github PK

View Code? Open in Web Editor NEW
810.0 11.0 70.0 79 KB

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

Home Page: https://jogendra.github.io/

License: MIT License

Ruby 4.93% Swift 95.07%
swift xcode cocoapods swift-framework pod podfile notifications swift-language ios animations swift5 hacktoberfest

badgehub's Introduction

BadgeHub

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

Version License Platform

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

License

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

badgehub's People

Contributors

amrangry avatar jogendra avatar matrixsenpai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

badgehub's Issues

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()

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?

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()
    }

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

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

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
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.