Skip to content

Instantly share code, notes, and snippets.

@zhangwc
Created August 28, 2013 15:43
Show Gist options
  • Save zhangwc/6367533 to your computer and use it in GitHub Desktop.
Save zhangwc/6367533 to your computer and use it in GitHub Desktop.
@implementation UIView (Badge)
- (void)addBadgeValue:(NSString *)badgeValue {
[self removeBadgeValue];
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:0];
item.badgeValue = badgeValue;
NSArray *array = [[NSArray alloc] initWithObjects:item, nil];
tabBar.items = array;
[item release];
[array release];
//寻找
for (UIView *viewTab in tabBar.subviews) {
for (UIView *subview in viewTab.subviews) {
NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)];
if ([strClassName isEqualToString:@"UITabBarButtonBadge"] ||
[strClassName isEqualToString:@"_UIBadgeView"]) {
//从原视图上移除
[subview removeFromSuperview];
//
[self addSubview:subview];
subview.frame = CGRectMake(self.frame.size.width-subview.frame.size.width, 0,
subview.frame.size.width, subview.frame.size.height);
[tabBar release];
return;
}
}
}
[tabBar release];
}
- (void)removeBadgeValue {
for (UIView *subview in self.subviews) {
NSString *strClassName = [NSString stringWithUTF8String:object_getClassName(subview)];
if ([strClassName isEqualToString:@"UITabBarButtonBadge"] ||
[strClassName isEqualToString:@"_UIBadgeView"]) {
[subview removeFromSuperview];
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment