├── Screenshots ├── screenshot1.png ├── screenshot2.png ├── screenshot3.png ├── screenshot4.png ├── screenshot5.png └── screenshot6.png ├── BadgedBarButtonItem ├── Assets.xcassets │ ├── Contents.json │ ├── cart.imageset │ │ ├── cart.png │ │ ├── cart@2x.png │ │ ├── cart@3x.png │ │ └── Contents.json │ ├── notif.imageset │ │ ├── notif.png │ │ ├── notif@2x.png │ │ ├── notif@3x.png │ │ └── Contents.json │ └── AppIcon.appiconset │ │ └── Contents.json ├── ViewController.swift ├── Info.plist ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── AppDelegate.swift └── BadgedBarButtonItem.swift ├── BadgedBarButtonItem.xcodeproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist ├── xcuserdata │ └── maksimartemov.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist └── project.pbxproj ├── .gitignore └── README.md /Screenshots/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/Screenshots/screenshot1.png -------------------------------------------------------------------------------- /Screenshots/screenshot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/Screenshots/screenshot2.png -------------------------------------------------------------------------------- /Screenshots/screenshot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/Screenshots/screenshot3.png -------------------------------------------------------------------------------- /Screenshots/screenshot4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/Screenshots/screenshot4.png -------------------------------------------------------------------------------- /Screenshots/screenshot5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/Screenshots/screenshot5.png -------------------------------------------------------------------------------- /Screenshots/screenshot6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/Screenshots/screenshot6.png -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/cart.imageset/cart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/BadgedBarButtonItem/Assets.xcassets/cart.imageset/cart.png -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/cart.imageset/cart@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/BadgedBarButtonItem/Assets.xcassets/cart.imageset/cart@2x.png -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/cart.imageset/cart@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/BadgedBarButtonItem/Assets.xcassets/cart.imageset/cart@3x.png -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/notif.imageset/notif.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/BadgedBarButtonItem/Assets.xcassets/notif.imageset/notif.png -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/notif.imageset/notif@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/BadgedBarButtonItem/Assets.xcassets/notif.imageset/notif@2x.png -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/notif.imageset/notif@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iosdevmax/BadgedBarButtonItem/HEAD/BadgedBarButtonItem/Assets.xcassets/notif.imageset/notif@3x.png -------------------------------------------------------------------------------- /BadgedBarButtonItem.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BadgedBarButtonItem.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BadgedBarButtonItem.xcodeproj/xcuserdata/maksimartemov.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | BadgedBarButtonItem.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/cart.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "cart.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "cart@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "filename" : "cart@3x.png", 16 | "scale" : "3x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/notif.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "notif.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "notif@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "filename" : "notif@3x.png", 16 | "scale" : "3x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /BadgedBarButtonItem/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // BadgedBarButtonItem 4 | // 5 | // Created by Maksim Artemov on 17/07/2019. 6 | // Copyright © 2019 Max. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | let btn = BadgedButtonItem(with: UIImage(named: "notif")) 14 | 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | 18 | btn.badgeTextColor = .black 19 | btn.badgeTintColor = .yellow 20 | btn.position = .right 21 | btn.hasBorder = true 22 | btn.borderColor = .red 23 | btn.badgeSize = .medium 24 | btn.badgeAnimation = true 25 | 26 | self.navigationItem.rightBarButtonItem = btn 27 | 28 | btn.tapAction = { 29 | self.btn.setBadge(with: 4) 30 | } 31 | 32 | DispatchQueue.main.asyncAfter(deadline: .now() + 1) { 33 | self.btn.setBadge(with: 66) 34 | 35 | } 36 | DispatchQueue.main.asyncAfter(deadline: .now() + 2) { 37 | self.btn.setBadge(with: 1) 38 | 39 | } 40 | DispatchQueue.main.asyncAfter(deadline: .now() + 3) { 41 | self.btn.setBadge(with: 99) 42 | 43 | } 44 | DispatchQueue.main.asyncAfter(deadline: .now() + 4) { 45 | self.btn.setBadge(with: 88) 46 | 47 | } 48 | 49 | } 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /BadgedBarButtonItem/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /BadgedBarButtonItem/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | Jazzy/ 9 | node_modules/ 10 | .idea/ 11 | 12 | ## Various settings 13 | *.pbxuser 14 | !default.pbxuser 15 | *.mode1v3 16 | !default.mode1v3 17 | *.mode2v3 18 | !default.mode2v3 19 | *.perspectivev3 20 | !default.perspectivev3 21 | xcuserdata/ 22 | 23 | ## Other 24 | *.moved-aside 25 | *.xcuserstate 26 | .DS_Store 27 | npm-debug.log 28 | 29 | ## Obj-C/Swift specific 30 | *.hmap 31 | *.ipa 32 | *.dSYM.zip 33 | *.dSYM 34 | 35 | ## Docker 36 | RabbitMq/data/ 37 | 38 | ## Playgrounds 39 | timeline.xctimeline 40 | playground.xcworkspace 41 | 42 | # Swift Package Manager 43 | # 44 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 45 | # Packages/ 46 | .build/ 47 | 48 | # CocoaPods 49 | # 50 | # We recommend against adding the Pods directory to your .gitignore. However 51 | # you should judge for yourself, the pros and cons are mentioned at: 52 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 53 | # 54 | Pods/ 55 | 56 | # Carthage 57 | # 58 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 59 | # Carthage/Checkouts 60 | 61 | Carthage/Build 62 | 63 | # fastlane 64 | # 65 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 66 | # screenshots whenever they are needed. 67 | # For more information about the recommended setup visit: 68 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 69 | 70 | fastlane/report.xml 71 | fastlane/Preview.html 72 | fastlane/screenshots 73 | fastlane/test_output 74 | 75 | TOFS.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BadgedBarButtonItem 2 | 3 | UIBarButtonItem with a customizable badge for iOS applications 4 | 5 | 6 | #### Instructions to use: 7 | 1. Add BadgedBarButtonItem class to your project 8 | 9 | 2. Create a new object with your image: 10 | 11 | ``` 12 | let btn = BadgedButtonItem(with: UIImage(named: "your_image")) 13 | ``` 14 | 15 | 3. Add the button to the navigation bar 16 | 17 | ``` 18 | self.navigationItem.rightBarButtonItem = btn 19 | ``` 20 | 21 | 22 | 4. Customise badge appearance 23 | 24 | - Set a tint color ```btn.badgeTintColor = .blue``` 25 | 26 | 27 | - Set a text color ```btn.badgeTextColor = .green``` 28 | 29 | 30 | - Set badge position (left or right) ```btn.position = .left``` 31 | 32 | 33 | - Add a border to the badge ```btn.hasBorder = true``` 34 | 35 | 36 | - Remove a border from the badge ```btn.hasBorder = false``` 37 | 38 | 39 | - Set badge border color ```btn.borderColor = .red``` 40 | - Set bagde size ```btn.badgeSize = .medium``` 41 | - Set badge animation ```btn.badgeAnimation = true``` 42 | 43 | 5. Set the badge with a value 44 | 45 | ``` 46 | btn.setBadge(with: 1) 47 | ``` 48 | 49 | 6. Add an action to the button 50 | 51 | ``` 52 | btn.tapAction = { 53 | // do something 54 | 55 | } 56 | ``` 57 | -------------------------------------------------------------------------------- /BadgedBarButtonItem/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /BadgedBarButtonItem/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // BadgedBarButtonItem 4 | // 5 | // Created by Maksim Artemov on 17/07/2019. 6 | // Copyright © 2019 Max. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /BadgedBarButtonItem/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /BadgedBarButtonItem/BadgedBarButtonItem.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BadgedBarButtonItem.swift 3 | // BadgedBarButtonItem 4 | // 5 | // Created by Maksim Artemov on 17/07/2019. 6 | // Copyright © 2019 Max. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | public enum BadgePosition { 12 | case left 13 | case right 14 | } 15 | 16 | public enum BadgeSize { 17 | case extraSmall 18 | case small 19 | case medium 20 | case large 21 | } 22 | 23 | public class BadgedButtonItem: UIBarButtonItem { 24 | 25 | public func setBadge(with value: Int?) { 26 | self.badgeValue = value 27 | } 28 | 29 | public var badgeTintColor: UIColor? { 30 | didSet { 31 | lblBadge.backgroundColor = badgeTintColor 32 | } 33 | } 34 | 35 | public var badgeTextColor: UIColor? { 36 | didSet { 37 | lblBadge.textColor = badgeTextColor 38 | } 39 | } 40 | 41 | public var position: BadgePosition? = .right { 42 | didSet { 43 | if position == .left { 44 | self.lblBadge.frame = CGRect(x: 0, 45 | y: 0, 46 | width: badgeRadius * 2, 47 | height: badgeRadius * 2) 48 | } else { 49 | self.lblBadge.frame = CGRect(x: filterBtn.frame.maxX - badgeRadius * 2, 50 | y: 0, 51 | width: badgeRadius * 2, 52 | height: badgeRadius * 2) 53 | } 54 | } 55 | } 56 | 57 | public var hasBorder: Bool? { 58 | didSet { 59 | if hasBorder == true { 60 | lblBadge.layer.borderWidth = 1.0 61 | } 62 | } 63 | } 64 | 65 | public var borderColor: UIColor? = .black { 66 | didSet { 67 | lblBadge.layer.borderColor = borderColor?.cgColor 68 | } 69 | } 70 | 71 | public var badgeSize: BadgeSize = .medium { 72 | didSet { 73 | switch badgeSize { 74 | case .extraSmall: 75 | self.badgeRadius = 6.0 76 | case .small: 77 | self.badgeRadius = 7.0 78 | case .medium: 79 | self.badgeRadius = 8.0 80 | case .large: 81 | self.badgeRadius = 9.0 82 | } 83 | } 84 | } 85 | 86 | public var badgeAnimation: Bool? = false { 87 | didSet { 88 | self.isAnimated = badgeAnimation 89 | } 90 | } 91 | 92 | public var tapAction: (() -> Void)? 93 | 94 | private var badgeValue: Int? { 95 | didSet { 96 | if let value = badgeValue, 97 | value > 0 { 98 | 99 | // reducing font size if the value has two digits 100 | if "\(value)".count > 1 { 101 | lblBadge.font = UIFont.systemFont(ofSize: twoDigitsFontSize) 102 | } else { 103 | lblBadge.font = UIFont.systemFont(ofSize: oneDigitFontSize) 104 | } 105 | 106 | lblBadge.isHidden = false 107 | lblBadge.text = "\(value)" 108 | animateBadge(isAnimated) 109 | } else { 110 | lblBadge.isHidden = true 111 | } 112 | } 113 | } 114 | 115 | 116 | private let filterBtn = UIButton() 117 | private let lblBadge = UILabel() 118 | private var isAnimated: Bool? = false 119 | private var badgeRadius: CGFloat = 8.0 { 120 | didSet { 121 | self.lblBadge.layer.cornerRadius = badgeRadius 122 | } 123 | } 124 | 125 | private var oneDigitFontSize: CGFloat { 126 | return badgeRadius + 1 127 | } 128 | private var twoDigitsFontSize: CGFloat { 129 | return badgeRadius 130 | } 131 | 132 | override init() { 133 | super.init() 134 | setup() 135 | } 136 | 137 | init(with image: UIImage?) { 138 | super.init() 139 | setup(image: image) 140 | } 141 | 142 | required init?(coder aDecoder: NSCoder) { 143 | super.init(coder: aDecoder) 144 | setup() 145 | } 146 | 147 | private func setup(image: UIImage? = nil) { 148 | 149 | self.filterBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 150 | self.filterBtn.adjustsImageWhenHighlighted = false 151 | let scaledImage = UIImage.imageWithImage(image, scaledToSize: CGSize(width: 20, height: 20)) 152 | self.filterBtn.setImage(scaledImage?.withRenderingMode(.alwaysOriginal), for: .normal) 153 | self.filterBtn.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) 154 | 155 | self.lblBadge.frame = CGRect(x: filterBtn.frame.maxX - badgeRadius * 2, 156 | y: 0, 157 | width: badgeRadius * 2, 158 | height: badgeRadius * 2) 159 | self.lblBadge.backgroundColor = .red 160 | self.lblBadge.clipsToBounds = true 161 | self.lblBadge.layer.cornerRadius = badgeRadius 162 | self.lblBadge.textColor = .white 163 | self.lblBadge.font = UIFont.systemFont(ofSize: 10) 164 | self.lblBadge.textAlignment = .center 165 | self.lblBadge.isHidden = true 166 | self.lblBadge.minimumScaleFactor = 0.5 167 | self.lblBadge.adjustsFontSizeToFitWidth = true 168 | self.filterBtn.addSubview(lblBadge) 169 | self.customView = filterBtn 170 | } 171 | 172 | @objc private func buttonPressed() { 173 | if let action = tapAction { 174 | action() 175 | } 176 | } 177 | 178 | private func animateBadge(_ animate: Bool?) { 179 | guard animate == true else { return } 180 | lblBadge.transform = CGAffineTransform(scaleX: 1.2, y: 1.2) 181 | UIView.animate(withDuration: 0.4, 182 | delay: 0, 183 | usingSpringWithDamping: 0.2, 184 | initialSpringVelocity: 3, 185 | options: [.curveLinear], 186 | animations: { [weak self] in 187 | 188 | guard let strongSelf = self else { return } 189 | strongSelf.lblBadge.transform = .identity 190 | 191 | }) { [weak self] (finished) in 192 | 193 | guard let strongSelf = self else { return } 194 | if !finished { 195 | strongSelf.lblBadge.transform = .identity 196 | } 197 | } 198 | } 199 | 200 | } 201 | 202 | /// Takes an image and returns a new one identical but resized. 203 | private extension UIImage { 204 | static func imageWithImage(_ image: UIImage?, scaledToSize newSize: CGSize) -> UIImage? { 205 | guard let image = image else { return nil } 206 | UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 207 | image.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: newSize.width, height: newSize.height))) 208 | let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 209 | UIGraphicsEndImageContext() 210 | return newImage 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /BadgedBarButtonItem.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C8F7845D22DF4E1500A7D6EF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8F7845C22DF4E1500A7D6EF /* AppDelegate.swift */; }; 11 | C8F7845F22DF4E1500A7D6EF /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8F7845E22DF4E1500A7D6EF /* ViewController.swift */; }; 12 | C8F7846222DF4E1500A7D6EF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C8F7846022DF4E1500A7D6EF /* Main.storyboard */; }; 13 | C8F7846422DF4E1700A7D6EF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C8F7846322DF4E1700A7D6EF /* Assets.xcassets */; }; 14 | C8F7846722DF4E1700A7D6EF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C8F7846522DF4E1700A7D6EF /* LaunchScreen.storyboard */; }; 15 | C8F7846F22DF4E3500A7D6EF /* BadgedBarButtonItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8F7846E22DF4E3500A7D6EF /* BadgedBarButtonItem.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | C8F7845922DF4E1500A7D6EF /* BadgedBarButtonItem.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BadgedBarButtonItem.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | C8F7845C22DF4E1500A7D6EF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | C8F7845E22DF4E1500A7D6EF /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | C8F7846122DF4E1500A7D6EF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | C8F7846322DF4E1700A7D6EF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | C8F7846622DF4E1700A7D6EF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | C8F7846822DF4E1700A7D6EF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | C8F7846E22DF4E3500A7D6EF /* BadgedBarButtonItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BadgedBarButtonItem.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | C8F7845622DF4E1500A7D6EF /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | C8F7845022DF4E1500A7D6EF = { 41 | isa = PBXGroup; 42 | children = ( 43 | C8F7845B22DF4E1500A7D6EF /* BadgedBarButtonItem */, 44 | C8F7845A22DF4E1500A7D6EF /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | C8F7845A22DF4E1500A7D6EF /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | C8F7845922DF4E1500A7D6EF /* BadgedBarButtonItem.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | C8F7845B22DF4E1500A7D6EF /* BadgedBarButtonItem */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | C8F7845C22DF4E1500A7D6EF /* AppDelegate.swift */, 60 | C8F7845E22DF4E1500A7D6EF /* ViewController.swift */, 61 | C8F7846022DF4E1500A7D6EF /* Main.storyboard */, 62 | C8F7846322DF4E1700A7D6EF /* Assets.xcassets */, 63 | C8F7846522DF4E1700A7D6EF /* LaunchScreen.storyboard */, 64 | C8F7846822DF4E1700A7D6EF /* Info.plist */, 65 | C8F7846E22DF4E3500A7D6EF /* BadgedBarButtonItem.swift */, 66 | ); 67 | path = BadgedBarButtonItem; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | C8F7845822DF4E1500A7D6EF /* BadgedBarButtonItem */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = C8F7846B22DF4E1700A7D6EF /* Build configuration list for PBXNativeTarget "BadgedBarButtonItem" */; 76 | buildPhases = ( 77 | C8F7845522DF4E1500A7D6EF /* Sources */, 78 | C8F7845622DF4E1500A7D6EF /* Frameworks */, 79 | C8F7845722DF4E1500A7D6EF /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = BadgedBarButtonItem; 86 | productName = BadgedBarButtonItem; 87 | productReference = C8F7845922DF4E1500A7D6EF /* BadgedBarButtonItem.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | C8F7845122DF4E1500A7D6EF /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 1020; 97 | LastUpgradeCheck = 1020; 98 | ORGANIZATIONNAME = Max; 99 | TargetAttributes = { 100 | C8F7845822DF4E1500A7D6EF = { 101 | CreatedOnToolsVersion = 10.2.1; 102 | }; 103 | }; 104 | }; 105 | buildConfigurationList = C8F7845422DF4E1500A7D6EF /* Build configuration list for PBXProject "BadgedBarButtonItem" */; 106 | compatibilityVersion = "Xcode 9.3"; 107 | developmentRegion = en; 108 | hasScannedForEncodings = 0; 109 | knownRegions = ( 110 | en, 111 | Base, 112 | ); 113 | mainGroup = C8F7845022DF4E1500A7D6EF; 114 | productRefGroup = C8F7845A22DF4E1500A7D6EF /* Products */; 115 | projectDirPath = ""; 116 | projectRoot = ""; 117 | targets = ( 118 | C8F7845822DF4E1500A7D6EF /* BadgedBarButtonItem */, 119 | ); 120 | }; 121 | /* End PBXProject section */ 122 | 123 | /* Begin PBXResourcesBuildPhase section */ 124 | C8F7845722DF4E1500A7D6EF /* Resources */ = { 125 | isa = PBXResourcesBuildPhase; 126 | buildActionMask = 2147483647; 127 | files = ( 128 | C8F7846722DF4E1700A7D6EF /* LaunchScreen.storyboard in Resources */, 129 | C8F7846422DF4E1700A7D6EF /* Assets.xcassets in Resources */, 130 | C8F7846222DF4E1500A7D6EF /* Main.storyboard in Resources */, 131 | ); 132 | runOnlyForDeploymentPostprocessing = 0; 133 | }; 134 | /* End PBXResourcesBuildPhase section */ 135 | 136 | /* Begin PBXSourcesBuildPhase section */ 137 | C8F7845522DF4E1500A7D6EF /* Sources */ = { 138 | isa = PBXSourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | C8F7845F22DF4E1500A7D6EF /* ViewController.swift in Sources */, 142 | C8F7845D22DF4E1500A7D6EF /* AppDelegate.swift in Sources */, 143 | C8F7846F22DF4E3500A7D6EF /* BadgedBarButtonItem.swift in Sources */, 144 | ); 145 | runOnlyForDeploymentPostprocessing = 0; 146 | }; 147 | /* End PBXSourcesBuildPhase section */ 148 | 149 | /* Begin PBXVariantGroup section */ 150 | C8F7846022DF4E1500A7D6EF /* Main.storyboard */ = { 151 | isa = PBXVariantGroup; 152 | children = ( 153 | C8F7846122DF4E1500A7D6EF /* Base */, 154 | ); 155 | name = Main.storyboard; 156 | sourceTree = ""; 157 | }; 158 | C8F7846522DF4E1700A7D6EF /* LaunchScreen.storyboard */ = { 159 | isa = PBXVariantGroup; 160 | children = ( 161 | C8F7846622DF4E1700A7D6EF /* Base */, 162 | ); 163 | name = LaunchScreen.storyboard; 164 | sourceTree = ""; 165 | }; 166 | /* End PBXVariantGroup section */ 167 | 168 | /* Begin XCBuildConfiguration section */ 169 | C8F7846922DF4E1700A7D6EF /* Debug */ = { 170 | isa = XCBuildConfiguration; 171 | buildSettings = { 172 | ALWAYS_SEARCH_USER_PATHS = NO; 173 | CLANG_ANALYZER_NONNULL = YES; 174 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 175 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 176 | CLANG_CXX_LIBRARY = "libc++"; 177 | CLANG_ENABLE_MODULES = YES; 178 | CLANG_ENABLE_OBJC_ARC = YES; 179 | CLANG_ENABLE_OBJC_WEAK = YES; 180 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 181 | CLANG_WARN_BOOL_CONVERSION = YES; 182 | CLANG_WARN_COMMA = YES; 183 | CLANG_WARN_CONSTANT_CONVERSION = YES; 184 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 186 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 187 | CLANG_WARN_EMPTY_BODY = YES; 188 | CLANG_WARN_ENUM_CONVERSION = YES; 189 | CLANG_WARN_INFINITE_RECURSION = YES; 190 | CLANG_WARN_INT_CONVERSION = YES; 191 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 192 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 193 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 194 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 195 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 196 | CLANG_WARN_STRICT_PROTOTYPES = YES; 197 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 198 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 199 | CLANG_WARN_UNREACHABLE_CODE = YES; 200 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 201 | CODE_SIGN_IDENTITY = "iPhone Developer"; 202 | COPY_PHASE_STRIP = NO; 203 | DEBUG_INFORMATION_FORMAT = dwarf; 204 | ENABLE_STRICT_OBJC_MSGSEND = YES; 205 | ENABLE_TESTABILITY = YES; 206 | GCC_C_LANGUAGE_STANDARD = gnu11; 207 | GCC_DYNAMIC_NO_PIC = NO; 208 | GCC_NO_COMMON_BLOCKS = YES; 209 | GCC_OPTIMIZATION_LEVEL = 0; 210 | GCC_PREPROCESSOR_DEFINITIONS = ( 211 | "DEBUG=1", 212 | "$(inherited)", 213 | ); 214 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 215 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 216 | GCC_WARN_UNDECLARED_SELECTOR = YES; 217 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 218 | GCC_WARN_UNUSED_FUNCTION = YES; 219 | GCC_WARN_UNUSED_VARIABLE = YES; 220 | IPHONEOS_DEPLOYMENT_TARGET = 12.2; 221 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 222 | MTL_FAST_MATH = YES; 223 | ONLY_ACTIVE_ARCH = YES; 224 | SDKROOT = iphoneos; 225 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 226 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 227 | }; 228 | name = Debug; 229 | }; 230 | C8F7846A22DF4E1700A7D6EF /* Release */ = { 231 | isa = XCBuildConfiguration; 232 | buildSettings = { 233 | ALWAYS_SEARCH_USER_PATHS = NO; 234 | CLANG_ANALYZER_NONNULL = YES; 235 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 236 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 237 | CLANG_CXX_LIBRARY = "libc++"; 238 | CLANG_ENABLE_MODULES = YES; 239 | CLANG_ENABLE_OBJC_ARC = YES; 240 | CLANG_ENABLE_OBJC_WEAK = YES; 241 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 242 | CLANG_WARN_BOOL_CONVERSION = YES; 243 | CLANG_WARN_COMMA = YES; 244 | CLANG_WARN_CONSTANT_CONVERSION = YES; 245 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 246 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 247 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 248 | CLANG_WARN_EMPTY_BODY = YES; 249 | CLANG_WARN_ENUM_CONVERSION = YES; 250 | CLANG_WARN_INFINITE_RECURSION = YES; 251 | CLANG_WARN_INT_CONVERSION = YES; 252 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 253 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 254 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 255 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 256 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 257 | CLANG_WARN_STRICT_PROTOTYPES = YES; 258 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 259 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 260 | CLANG_WARN_UNREACHABLE_CODE = YES; 261 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 262 | CODE_SIGN_IDENTITY = "iPhone Developer"; 263 | COPY_PHASE_STRIP = NO; 264 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 265 | ENABLE_NS_ASSERTIONS = NO; 266 | ENABLE_STRICT_OBJC_MSGSEND = YES; 267 | GCC_C_LANGUAGE_STANDARD = gnu11; 268 | GCC_NO_COMMON_BLOCKS = YES; 269 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 270 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 271 | GCC_WARN_UNDECLARED_SELECTOR = YES; 272 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 273 | GCC_WARN_UNUSED_FUNCTION = YES; 274 | GCC_WARN_UNUSED_VARIABLE = YES; 275 | IPHONEOS_DEPLOYMENT_TARGET = 12.2; 276 | MTL_ENABLE_DEBUG_INFO = NO; 277 | MTL_FAST_MATH = YES; 278 | SDKROOT = iphoneos; 279 | SWIFT_COMPILATION_MODE = wholemodule; 280 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 281 | VALIDATE_PRODUCT = YES; 282 | }; 283 | name = Release; 284 | }; 285 | C8F7846C22DF4E1700A7D6EF /* Debug */ = { 286 | isa = XCBuildConfiguration; 287 | buildSettings = { 288 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 289 | CODE_SIGN_STYLE = Automatic; 290 | DEVELOPMENT_TEAM = UL386KE9LP; 291 | INFOPLIST_FILE = BadgedBarButtonItem/Info.plist; 292 | LD_RUNPATH_SEARCH_PATHS = ( 293 | "$(inherited)", 294 | "@executable_path/Frameworks", 295 | ); 296 | PRODUCT_BUNDLE_IDENTIFIER = com.syngmaster.BadgedBarButtonItem; 297 | PRODUCT_NAME = "$(TARGET_NAME)"; 298 | SWIFT_VERSION = 5.0; 299 | TARGETED_DEVICE_FAMILY = "1,2"; 300 | }; 301 | name = Debug; 302 | }; 303 | C8F7846D22DF4E1700A7D6EF /* Release */ = { 304 | isa = XCBuildConfiguration; 305 | buildSettings = { 306 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 307 | CODE_SIGN_STYLE = Automatic; 308 | DEVELOPMENT_TEAM = UL386KE9LP; 309 | INFOPLIST_FILE = BadgedBarButtonItem/Info.plist; 310 | LD_RUNPATH_SEARCH_PATHS = ( 311 | "$(inherited)", 312 | "@executable_path/Frameworks", 313 | ); 314 | PRODUCT_BUNDLE_IDENTIFIER = com.syngmaster.BadgedBarButtonItem; 315 | PRODUCT_NAME = "$(TARGET_NAME)"; 316 | SWIFT_VERSION = 5.0; 317 | TARGETED_DEVICE_FAMILY = "1,2"; 318 | }; 319 | name = Release; 320 | }; 321 | /* End XCBuildConfiguration section */ 322 | 323 | /* Begin XCConfigurationList section */ 324 | C8F7845422DF4E1500A7D6EF /* Build configuration list for PBXProject "BadgedBarButtonItem" */ = { 325 | isa = XCConfigurationList; 326 | buildConfigurations = ( 327 | C8F7846922DF4E1700A7D6EF /* Debug */, 328 | C8F7846A22DF4E1700A7D6EF /* Release */, 329 | ); 330 | defaultConfigurationIsVisible = 0; 331 | defaultConfigurationName = Release; 332 | }; 333 | C8F7846B22DF4E1700A7D6EF /* Build configuration list for PBXNativeTarget "BadgedBarButtonItem" */ = { 334 | isa = XCConfigurationList; 335 | buildConfigurations = ( 336 | C8F7846C22DF4E1700A7D6EF /* Debug */, 337 | C8F7846D22DF4E1700A7D6EF /* Release */, 338 | ); 339 | defaultConfigurationIsVisible = 0; 340 | defaultConfigurationName = Release; 341 | }; 342 | /* End XCConfigurationList section */ 343 | }; 344 | rootObject = C8F7845122DF4E1500A7D6EF /* Project object */; 345 | } 346 | --------------------------------------------------------------------------------