您的位置:首页 > 移动开发 > IOS开发

iOS 中的BadgeView(推送消息的那个圆形图标)

2014-11-06 09:58 232 查看
1.直接用系统的
想要在任意一个位置加图标  思路为先从系统的tabbar里把badgeView取出来 然后再放到其他view上
- (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;

  //寻找
 
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"]) {
       
//
        [self.view
addSubview:subview];
        subview.frame =
CGRectMake(self.view.frame.size.width-subview.frame.size.width,
0,
                                   subview.frame.size.width, subview.frame.size.height);
       
return subview;
      }
    }
  }

  return nil;
}

//从原视图上移除
- (void)removeBadgeValue
{
 
//
 
for (UIView *subview
in self.view.subviews) {

    NSString *strClassName = [NSString
stringWithUTF8String:object_getClassName(subview)];
   
if ([strClassName
isEqualToString:@"UITabBarButtonBadge"] ||
        [strClassName
isEqualToString:@"_UIBadgeView"]) {

      [subview removeFromSuperview];
     
break;
    }
  }
}

2.第三方JBBadgeView https://github.com/JaviSoto/JSBadgeView
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uiview