Created
August 28, 2013 15:43
-
-
Save zhangwc/6367533 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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