This comment has been minimized.
This comment has been minimized.
This one supports double digits, but not triple. Just a small change on the badge width and label offset, but I'll post the whole thing:
|
This comment has been minimized.
This comment has been minimized.
Badge don't seem to work on UIBarButtonItem that are on UIToolbar, any ideas? |
This comment has been minimized.
This comment has been minimized.
I've been searching for nearly a full day and implemented several others' attempts..This one is the best and easiest way!!! Thank you so much!! |
This comment has been minimized.
This comment has been minimized.
I hate to ask, but how do I use this? I am new to IOS development. I have added the swift file and created a navigation button like this: navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(lockedButton)) Not sure where I can use the addBadge method? I tried referencing it like this: navigationItem.rightBarButtonItem = UIBarButtonItem.addBadge() But there is a red line through the addBadge option as I type, and it doesn't like it if I add it in manually... |
This comment has been minimized.
This comment has been minimized.
Ok, sorry, I found the answer. For anyone else looking, you need to reference the button in a variable and add the method on that... navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(lockedButton))
|
This comment has been minimized.
This comment has been minimized.
This was a very useful starting point. I've taken the Swift 3 version and enhanced the function so it supports text (not just numbers). It also draws a rounded rectangle instead of a circle, to better support wider strings. The code dynamically calculates the badge size based on the badge text. Finally, I've changed the signature to be setBadge. If the badge is null or empty, then the badge will be removed.
|
This comment has been minimized.
This comment has been minimized.
I have tested with Xcode 8 and Swift 3. When device orientation changes, badge is removed. I checked the view hierarchy and observed that badge is gone. I hope someone writes a fix for that. |
This comment has been minimized.
This comment has been minimized.
Thanks guys, very useful. |
This comment has been minimized.
This comment has been minimized.
@tosbaha |
This comment has been minimized.
This comment has been minimized.
thanks its work fine. |
This comment has been minimized.
This comment has been minimized.
I needed same thing for a regular UIButton,
then wherever
then
|
This comment has been minimized.
This comment has been minimized.
Hi all, after doing this in viewDidLoad (and adding the extension code), i can see the bar button item in the navigation bar but not the badge :( let inboxButton:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "inbox"), style: .plain, target: self, action: #selector(presentInbox)) Has someone a simple working example? Thank you very much Guys :) |
This comment has been minimized.
This comment has been minimized.
https://gist.github.com/mohn93/bd950eeed82749b2ecad40ba3d4d0f1d This fork has more features, hope it helps |
This comment has been minimized.
This comment has been minimized.
Swift 4 version of the same
|
This comment has been minimized.
This comment has been minimized.
Has anyone been able to use this with xcode 9? This code does not work anymore: |
This comment has been minimized.
This comment has been minimized.
works for me 9.0.1 |
This comment has been minimized.
This comment has been minimized.
** #guard let view = self.value(forKey: "view") as? UIView else { return }** here view is getting value nil because of which it does not show badge how do i fix this? |
This comment has been minimized.
This comment has been minimized.
@zeeip6, I faced the same issue. It guess the name of the key for view might be changed to something else and because of this its returning nil. The workaround I did for that was to use customView as below guard let view = self.customView else { return } and initialize a BarButtonItem by giving an UIButton as its custom view. let bellButton = UIButton(type: .custom) let notificationBarButton = UIBarButtonItem(customView: bellButton) Note: Offset needs to be adjusted as required when adding a badge. |
This comment has been minimized.
This comment has been minimized.
@febinfathah code is working, but in my case badge was a truncated a bit from the top and I had to add an offset, not a big deal. But then I realized that badge is drawing behind icon, which is unacceptable. I fixed it by adding the following line on the bottom off "addBadge":
So, the new code is:
Tested on both iPhone and iPad with iOS 11.1. Maintains its appearance after changing orientation as well. |
This comment has been minimized.
This comment has been minimized.
Hello guys, I was trying to implement this using Xamarin but I always stop here because the view returned is always null. Am I missing out something or do I not understand how swift works? Here's where the error is and my c# implementation of that same code. |
This comment has been minimized.
This comment has been minimized.
Hi, |
This comment has been minimized.
This comment has been minimized.
So if you want to display the badge, you would have to set the badge outside of viewDidLoad and you should see the badge. It seems setting the badge in viewDidAppear works for me there. If you have been setting the badge in viewDidLoad, in iOS11 it handles it differently. Any related UI work will break or wont work. Try setting the badge outside of viewDidLoad and see if that works. I hope this helps |
This comment has been minimized.
This comment has been minimized.
As others have mentioned the view will be nil if you set the badge on viewDidLoad on iOS 11 and Xcode 9. To solve this just set the badge to the bar button Item in the " viewDidAppear" function. |
This comment has been minimized.
This comment has been minimized.
Thank you all for the solution, neat and concise. |
This comment has been minimized.
This comment has been minimized.
Instead of global handle var, it is better to use static struct, IMHO private struct AssociatedKey {
static var badgeLayer = "badgeLayer"
}
fileprivate var badgeLayer: CAShapeLayer? {
get {
return objc_getAssociatedObject(self, &AssociatedKey.badgeLayer) as? CAShapeLayer
}
set {
if let newValue = newValue {
objc_setAssociatedObject(
self,
&AssociatedKey.badgeLayer,
newValue as CAShapeLayer?,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
}
} |
This comment has been minimized.
This comment has been minimized.
Does anyone know why this doesn't work with back button? |
This comment has been minimized.
This comment has been minimized.
Its not working in Swift4. |
This comment has been minimized.
This comment has been minimized.
Different approach and works with Swift 4 and various view controller states: https://gist.github.com/yonat/75a0f432d791165b1fd6 |
This comment has been minimized.
This comment has been minimized.
I have used this for tabItem but I want to show the layer on top of tabImage but layer is added behind it. Any way through which it can be attained? |
This comment has been minimized.
This comment has been minimized.
This doesn't work unless it is a custom uibarbutton unforunately. :( |
This comment has been minimized.
This comment has been minimized.
u can get it by the property "badgeLayer" |
This comment has been minimized.
This comment has been minimized.
u can extension a button to do it , c the #ugenlik comment |
This comment has been minimized.
This comment has been minimized.
swift 4.2
|
This comment has been minimized.
This comment has been minimized.
This is not working. |
This comment has been minimized.
This comment has been minimized.
ok this code is not work for me: self.navigationItem.setLeftBarButton(btnNoti, animated: true) |
This comment has been minimized.
This comment has been minimized.
Update to Swift 5 + fix SwiftLint warnings:
|
This comment has been minimized.
This comment has been minimized.
Working in swift 5, same code of @adam-leitgeb , but i only wanted the red dot, without the number, so if anyone is looking for the same, i leave the code here private var handle: UInt8 = 0
} // MARK: - Utilities |
This comment has been minimized.
Updated for Swift 3. It would be awesome if someone could make this support more than 1 number in the badge.