首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

在任意UIView下显示Badge(兼容IOS6)

2014-06-14 
在任意UIView上显示Badge(兼容IOS6)之前写过一篇文章在任意UIView上显示Badge,但IOS6上失败了,原来是苹果

在任意UIView上显示Badge(兼容IOS6)

之前写过一篇文章在任意UIView上显示Badge,但IOS6上失败了,原来是苹果将Badge的类名改了,所以这里作下兼容,源码如下:

- (UIView *)showBadgeValue:(NSString *)strBadgeValue{    UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:0];    item.badgeValue = strBadgeValue;    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 subview;            }        }    }    [tabBar release];    return nil;}- (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;        }    }}

一个方法是显示Badge用的,一个是隐藏(移除)用的。

1楼yjh4866昨天 22:08
把这两个方法写成UIView的分类方法哦

热点排行