消息通知_小红点

2016-11-05  本文已影响0人  HuangLinWang
     // 消息数量
    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];
    // 自定义导航栏左侧按钮
      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;
上一篇 下一篇

猜你喜欢

热点阅读