- Simply attach BadgeController to any UIView's subclasses and easily control the badge.
- Customize size, position, background color, text color, border width, border color and animation of the badge.
- If you are using CocoaPods add this text to your Podfile and run
pod install
.
pod 'BadgeControl'
- Add BadgeController.swift, BadgeView.swift, BadgeCenterPosition.swift and BadgeAnimations.swift to your project.
Add import BadgeControl
to your source code (unless you used the file setup method).
Simply attach BadgeController to your UIView (or its subclass).
let badge = BadgeController(for: myUIView)
Add an empty badge and present it with animation.
badge.addOrReplaceCurrent(animated: true)
Add a badge with text and present it with animation.
badge.addOrReplaceCurrent(with text: "1", animated: true)
Remove badge from its view.
badge.remove(animated: true)
Simply increment or decrement the value on your badge (if it is numeric) and present it with animation.
badge.increment(animated: true)
badge.decrement(animated: true)
You can also set animateOnlyWhenBadgeIsNotYetPresent = true
if you don't want animation when badge is already present on the view.
You can customize badge's text font, background color, text color, border width, border color, size, center position and animation.
badge.badgeTextFont = UIFont.systemFont(ofSize: 15)
badge.badgeBackgroundColor = UIColor.blue
badge.badgeTextColor = UIColor.yellow
badge.borderWidth = 3 // in px
badge.borderColor = UIColor.black // black is default
badge.badgeHeight = 20 // badge will have height of 20px
You can choose following center positions: upperLeftCorner, upperRightCorner, lowerLeftCorner, lowerRightCorner.
You can also choose custom position by calling .custom(x: Double, y: Double).
badge.centerPosition = .upperLeftCorner
badge.centerPosition = .custom(x: 10, y: 20)
You can choose one of the following animations:
badge.animation = BadgeAnimations.default
badge.animation = BadgeAnimations.leftRight
badge.animation = BadgeAnimations.rightLeft
badge.animation = BadgeAnimations.fadeIn
badge.animation = BadgeAnimations.rolling
You can also provide your own animation of type ((UIView) -> Void).
badge.animation = { badgeView in
...
}
You can use this initalizer with default values:
public init(for view: UIView,
in centerPosition: BadgeCenterPosition = .upperRightCorner,
badgeBackgroundColor: UIColor = .red,
badgeTextColor: UIColor = .white,
badgeTextFont: UIFont? = nil,
borderWidth: CGFloat = 0.0,
borderColor: UIColor = .black,
animation: ((UIView) -> Void)? = BadgeAnimations.defaultAnimation,
badgeHeight: Int? = nil
animateOnlyWhenBadgeIsNotYetPresent: Bool = false)
BadgeControl is released under the MIT License.
If you've found a bug or want to improve BadgeControl feel free to create an issue.