mikemtol / uibarbuttonitem-badge Goto Github PK
View Code? Open in Web Editor NEWUIBarButtonItem+Badge
UIBarButtonItem+Badge
UIBarButtonItem-Badge was working fine in iOS 8 but after upgrading to iOS 9 and Swift 2.0 the badge now shows up as a square. Anyone know a fix?
hi, the judgement of badgeValue in setBadgeVaule method is wrong!.
I sepearate it in two judgements:
// When changing the badge value check if we need to remove the badge
if (!badgeValue || [badgeValue isEqualToString:@""]) {
[self removeBadge];
}else if ([badgeValue isEqualToString:@"0"]){// && self.shouldHideBadgeAtZero error。
[self removeBadge];
}
or if (badgeValue || [badgeValue isEqualToString:@""] || [badgeValue isEqualToString:@"0"] &&self.shouldHideBadgeAtZero )
In my case, I customized the class by making the corner radius specifically 8 to have a rounded corner instead of a circle.
The config shouldHideBadgeAtZero
only works when set before the badge count.
self.settingsBtn.badgeValue = "0"
self.settingsBtn.shouldHideBadgeAtZero = true
will display a badge with value 0.
When changing shouldHideBadgeAtZero
it should take the current value into account and update.
swift code:
self.navigationItem.rightBarButtonItem?.badgeValue = "\(count)"
First time it can be updaed immediately. When the value changed the view only be updated after a few seconds.
It updated after about 7 seconds in rootviewconroller. But it even doen't work in pushed viewcontroller when the value changed.
I couldn't get this to work with Swift. The extensions on the buttons aren't recognized, such as badgeValue
isn't found on the button. I think because there are special characters in the namespace so I can't import it (import UIBarButtonItem-Badge
). Anyone got this to work with Swift that you can post a sample? I install it with CocoaPods.
Is your PodSpec working properly: I couldn't retrieve your project through cocoa-pod?
My podfile look like this:
source 'https://github.com/CocoaPods/Specs.git'
target "XEBTest" do
pod 'Reachability', '>3.2'>0.0.2'
pod 'MIBadgeButton', '
pod 'UIBarButtonItem-Badge', '~>0.0.1'
end
target "XEBTestTests" do
end
I have tried many different ways to send a "0" to the BadgeValue and no matter which way I send it, it displays a 0. I can only assume this is the case because this line of code:
[badgeValue isEqualToString:@"0"]
in the UIButton.m file does not seem to be running correctly. Even though I can not see any reason why it won't run correctly.
Here are the different ways I have tried to send a NSString amount to the Badge:
[NSString stringWithFormat:@"%ld", (long)updateNumber]
[NSString stringWithFormat:@"%d" ,updateNumber] //This way gives errors in the 64 bit environment
[NSString stringWithFormat:@"%lu", updateNumber]
[NSString stringWithFormat:@"%i", updateNumber]//Also more errors
[NSString stringWithFormat:@"%lu", (long)updateNumber]
None of the above work for me so I have had to add in code into my app that changes the "0" to a "".
I could be doing something else wrong but I can't see it.
By the way, this is a great badge icon addition to the UIButton and UIBarButton classes. I wish apple would add this code in so everyone could use it. Thank you for creating this!
Right now the badge looks different depending on when it's configured.
The configuration is only valid when applied after the badge count is set. The order should not matter.
self.settingsBtn.badgeValue = "\(count)"
let x = self.settingsBtn.badgeOriginX
let y = self.settingsBtn.badgeOriginY
self.settingsBtn.badgeOriginX = x - 10
self.settingsBtn.badgeOriginY = y + 3
self.settingsBtn.badgeFont = UIFont.systemFontOfSize(10)
self.settingsBtn.badgePadding = 4
is not the same as
let x = self.settingsBtn.badgeOriginX
let y = self.settingsBtn.badgeOriginY
self.settingsBtn.badgeOriginX = x - 10
self.settingsBtn.badgeOriginY = y + 3
self.settingsBtn.badgeFont = UIFont.systemFontOfSize(10)
self.settingsBtn.badgePadding = 4
self.settingsBtn.badgeValue = "\(count)"
Nice extension !
Would be great to be able to use this on UIButton
Hello, and thank you for this creation!
I'm using UIBarButtonItem-Badge with Swift and I can't seem to set the origin of the badge. It has no effect on the badge.
let button = UIBarButtonItem(image: buttonImage, style: .Plain, target: self, action: "buttonAction:")
button.shouldHideBadgeAtZero = true
button.badgeOriginX = -20
If I change it in badgeInit it works, but want to avoid modifying the original code.
After NSLogg'ing updateBadgeFrame it looks like it updates twice, the second time with the default value:
2015-03-04 12:59:00.367 myApp[123] Update badge frame x: -20.000000
2015-03-04 12:59:00.367 myApp[123] Update badge frame x: 24.000000
Thanks!
The badge is not showing on Xcode 8 working fine on Xcode 7
Please release a version.
Hi, i am using this control for showing badge on barbuttonitem. it works fine for the first controller or root controller in navigation controller stack. While pushing to another controller, I tried to show badge and I cannot see any effect there. Can you help me to resolve this issue. It's very urgent for me now.
One nice improvement that could be made to this library, is improving the flow in which the badge view gets built / laid out.
For instance, trying to add a badge to a UIButton
that is being built using Autolayout and not having a specific frame
set, then the badge is placed on the top left corner of the button by default.
Another example is if you set the badge properties before assigning the UIBarButtonItem
to the navigationItem.rightBarButtonItem
or navigationItem.rightBarButtonItems
properties, the superview
is nil
.
Would be nice to change the logic of the badge to wait until it's superview (UIBarButtonItem
or UIButton
) has been fully laid out.
Hi,
I'm using this library and find it very simple and useful.
I want to add a border to my badge (border color and border width).
i modified method refreshBadge
in the class UIBarButtonItem+Badge.m
as follow:
// Handle badge display when its properties have been changed (color, font, ...)
- (void)refreshBadge
{
// Change new attributes
self.badge.textColor = self.badgeTextColor;
self.badge.backgroundColor = self.badgeBGColor;
self.badge.font = self.badgeFont;
self.badge.layer.borderColor= [UIColor whiteColor].CGColor;
self.badge.layer.borderWidth= 1.5f;
....
}
So, I added these two lines:
self.badge.layer.borderColor= [UIColor whiteColor].CGColor;
self.badge.layer.borderWidth= 1.5f;
Please, could you add this in the library and make customizable.
Regards.
Hi,
I have used that code but it is not working in IOS 11.2 for bar button item. Can you please check that issue and update it?
Thanks
I have a UINavigationItem
which contains three UIBarButtonItem
.
I have used UIBarButtonItem+Badge
to show badge on the middle UIBarButtonItem
. Which appears perfectly fine.
Scenario for the problem that occurs is listed below.
Help me here. Thanks in advance.
If placed to YES, badge disappears for no apparent reason.
For some strange reason if I change from screen A to screen B using a slidemenu with the pushViewController instruction the badgeValue disappears, but if I turn off animations it stays there.
This is the method I'm using.
func openViewControllerBasedOnIdentifier(_ strIdentifier:String){
let destViewController : UIViewController = self.storyboard!.instantiateViewController(withIdentifier: strIdentifier)
let topViewController : UIViewController = self.navigationController!.topViewController!
if (topViewController.restorationIdentifier! == destViewController.restorationIdentifier!){
print("Same VC")
} else {
var numeroProductos = String(Carrito.numProd)
self.navigationController!.pushViewController(destViewController, animated: true)
}
}
If instead of using .pushViewController I perform a normal segue this doesn't happen, in either case I load the badgeValue again on viewDidLoad. Any ideas? I'm using swift 3.0
This happens when the device is rotated to landscape, and the UIBarButtonItem has a landscapeImagePhone property (for showing a different image in landscape on iPhone).
Once this happens, the badge disappears for good, rotating back to portrait and trying to show the badge doesn't work.
Hi,
I explain quickly my issue : i have two UIBarButtonItem added on a UINavigationItem :
self.navigationItem.rightBarButtonItems =
[NSArray arrayWithObjects:navRightButton, right2AddBarButtonItem, nil];
This allows me to have more than one button on the right hand side of the screen.
If i add a button made with your component, the badge doesn't appear.
If i do it the right way (only one button added via :
self.navigationItem.rightBarButtonItem = navRightButton;
it works like a charm...
Hope this a small adaptation...coz i find you'r component very convenient and well made...and i would really enjoy to use it
Thanks
It would be nice if you add podspec for CocoaPods, so ppl can use pods.
There's another solution for badges https://github.com/TanguyAladenise/BBBadgeBarButtonItem and it has podspec (and more stars).
Anyway, thank you!
I've got a (custom) uibutton as a barbuttonitem, and whenever I call .badgeValue or setBadgeValue it only gets updated after the view is reloaded (viewwillappear called)
Badge disappears when changing tintColor of item
Thats because internal view to which badge labels belongs recreated when tintColor changed.
i made workaround by checking superview of badge label:
-(UILabel_) badge {
UILabel_ lbl = objc_getAssociatedObject(self, &UIBarButtonItem_badgeKey);
if(lbl==nil || lbl.superview==nil || lbl.superview.superview==nil) {
lbl = [[UILabel alloc] initWithFrame:CGRectMake(self.badgeOriginX, self.badgeOriginY, 20, 20)];
[self setBadge:lbl];
[self badgeInit:lbl];
[self.customView addSubview:lbl];
lbl.textAlignment = NSTextAlignmentCenter;
}
return lbl;
}
also have to change badgeInit a little:
(void)badgeInit:(UIView*) badge
{
UIView *superview = nil;
CGFloat defaultOriginX = 0;
if (self.customView) {
superview = self.customView;
defaultOriginX = superview.frame.size.width - self.badge.frame.size.width/2;
// Avoids badge to be clipped when animating its scale
superview.clipsToBounds = NO;
} else if ([self respondsToSelector:@selector(view)] && [(id)self view]) {
superview = [(id)self view];
defaultOriginX = superview.frame.size.width - badge.frame.size.width;
}
[superview addSubview:badge];
// Default design initialization
self.badgeBGColor = [UIColor redColor];
self.badgeTextColor = [UIColor whiteColor];
self.badgeFont = [UIFont systemFontOfSize:12.0];
self.badgePadding = 6;
self.badgeMinSize = 8;
self.badgeOriginX = defaultOriginX;
self.badgeOriginY = -4;
self.shouldHideBadgeAtZero = YES;
self.shouldAnimateBadge = YES;
}
Hi, great work on this class. Can you please add a license for using it?
Please consider the MIT license as it's usually the easiest to work with for other developers.
MyCode:
button.badgeOriginX=30;
button.badge=@"2";
if seems happend on "self.badgeOriginX = self.frame.size.width - self.badge.frame.size.width/2", in the badgeInit
I implemented this library in my Swift project, and it works great. I really appreciate it.
The only problem I have is that tapping on the buttons with the badges sometime fails. I assume that it might be because badges do not accept touch gestures and the size of touchable area on the buttons becomes smaller.
Is possible to fix it?
Or is there any workaround?
When adding multiple UIBarButtonItems to the navigation bar using the rightBarButtonItems
property, the badge does not appear. This is a result of the bar button item which should display the badge having the customView
property being nil.
Is it possible to apply a fix for this? Would love to use this library to show simple badges, but my app always has multiple UIBarButtonItems in the navigation bar.
When the Button with badge is displayed on navigation bar for the initial loading time, it gets displayed correctly. But when navigate back to the screen from its child screens, it gets overlapped by the status bar !
I checked the frame of the BarButtonItem on both scenarios, but found it is the same !
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.