美文网首页
消息通知_小红点

消息通知_小红点

作者: HuangLinWang | 来源:发表于2016-11-05 17:57 被阅读0次
  • App的桌面应用图标上的消息提示


    Paste_Image.png
    • iOS8.0版本需要注册
      if ([[UIDevice currentDevice].systemVersion intValue] > 7.99 && [[UIDevice currentDevice].systemVersion intValue] <9.001){
      UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
      [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
      }
    • 9.0 之后的直接一句话即可
      [UIApplication sharedApplication].applicationIconBadgeNumber = 20;
  • 系统方法-> 给UITabBarController设置消息小红点


    Paste_Image.png
    • 方法不要在TabBarController的ViewDidLoad方法中调用,因为此时items为nil
      获取到tabBarControl的所有items
      NSArray *tabBarItems = self.tabBar.items;
      属性badgeValue(标记个数)就是消息的条数
      UITabBarItem *tabBarItem = [tabBarItems objectAtIndex:1];
      消息数量
      static NSInteger i = 20;
      i--;
      tabBarItem.badgeValue = [NSString stringWithFormat:@"%zd",i];
  • 自定义方法 ->给UITabBarController设置消息小红点
    • 思路:
      • 创建Button,计算frame放到对应的位置即可
    • 扩展
      • 你可以给Button添加一个属性num用来记录消息的数量.在给num属性设置值的时候,将消息的数量设置给Button的title即可
      • 注意不要让Button一直创建,最好让Button创建一次(自己针对自己的情况考虑吧)
     // 消息数量
    static NSInteger i = 20;
    i--;
    // Button的设置
    UIButton *button = [[UIButton alloc]init];
    // 关闭Button的交互
    self.btn.enabled = NO;
    [self.btn setBackgroundColor:[UIColor redColor]];
    // 设置Button的title
    [button setTitle:[NSString stringWithFormat:@"%zd",i] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:10];

    // 获取tabbarItame的frame
    CGRect tabFrame =self.tabBar.frame;
    float percentX = (1 +0.6) / self.tabBar.items.count;
    float percentY = 0.1;
    CGFloat x = percentX * tabFrame.size.width;
    CGFloat y = percentY * tabFrame.size.height;

    // 设置小红点图标的frame
    button.frame = CGRectMake(x, y, 16, 16);
    self.btn.layer.cornerRadius = 8;
    self.btn.layer.masksToBounds = YES;
    // 将小红点imageView添加到tabbar上面
    [self.tabBar addSubview:button];
  • 自定义方法 ->给UINavigationItem设置消息小红点


    Paste_Image.png
    • 思路自定义Button,给Button添加一个label.消息的数量设置给label的text属性即可
    // 自定义导航栏左侧按钮
      UIButton * leftBtn = [[UIButton alloc]init];
      leftBtn.frame = CGRectMake(0, 7, 60, 30);
      leftBtn.backgroundColor = [UIColor whiteColor];
    // 消息数量
      static NSInteger i = 20;
      i--;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 16, 16)];
    // label的属性设置
      label.text = @(i).description;
      label.font = [UIFont systemFontOfSize:11];
      label.backgroundColor = [UIColor redColor];
      label.textColor = [UIColor whiteColor];
    // 切角
      label.layer.cornerRadius = 8;
      label.layer.masksToBounds = YES;
    // 文字居中
      label.textAlignment = NSTextAlignmentCenter;
      label.frame = CGRectMake(leftBtn.frame.size.width - label.frame.size.width, 0, 16, 16);
    // 添加到Button中    
      [leftBtn addSubview:label];
    //  替换左侧的UIBarButtonItem
      UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
      self.navigationItem.leftBarButtonItem = leftItem;

相关文章

网友评论

      本文标题:消息通知_小红点

      本文链接:https://www.haomeiwen.com/subject/qecguttx.html