当前位置:编程学习 > wap >>

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

之前写过一篇文章在任意UIView上显示Badge,但IOS6上失败了,原来是苹果将Badge的类名改了,所以这里作下兼容,源码如下:
[plain] 
- (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; 
        } 
    } 
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,