Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在定制的UIBarbuttonite上佩戴徽章_Ios_Objective C_Xcode_Uibarbuttonitem_Navigationbar - Fatal编程技术网

Ios 如何在定制的UIBarbuttonite上佩戴徽章

Ios 如何在定制的UIBarbuttonite上佩戴徽章,ios,objective-c,xcode,uibarbuttonitem,navigationbar,Ios,Objective C,Xcode,Uibarbuttonitem,Navigationbar,我有一个带有两个按钮的导航栏,一个是后退按钮,另一个是聊天符号 我这样写代码: UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain

我有一个带有两个按钮的导航栏,一个是后退按钮,另一个是聊天符号

我这样写代码:

UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back.png"]
                                                      style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:@selector(goBackToPreviousView)];

self.navigationItem.leftBarButtonItem=_btn;
self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];


UIBarButtonItem *_btn2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"chat.png"]
                                                      style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:@selector(startChat)];

self.navigationItem.rightBarButtonItem=_btn2;
self.navigationItem.rightBarButtonItem.tintColor = [Utility colorWithHexValue:CyanBlue];
我遇到的问题是,每当聊天中有一些我没有看到的新消息时,聊天按钮上应该有一个类似徽章或自定义标签,以指示您有多少新消息

我该怎么做呢?

试试这个定制的方法


  • 你可以试试这个类别


    在iOS 8中,我可以通过更改参数值来实现这一点

                self.messageButton.badgeValue = @"5";
    
    其中消息按钮是UIBarButton

    您必须将这两个文件包含到xcode中 伊巴布托教派+徽章 伊巴布托教派+徽章.m

    可以在这里找到

    你好! 这是我的版本-简单且没有任何问题:

    navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 64)];
    [self.view addSubview:navBar];
    
    navItem    = [[UINavigationItem alloc] initWithTitle:NSLocalizedString(@"Meetings", nil)];
    
    [navBar pushNavigationItem:navItem animated:NO];
        UIButton *chat = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [chat setTitle:@"Chat" forState: UIControlStateNormal];
        UILabel *badge  = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 20, 20)];
    badge.textColor = [UIColor darkGrayColor];
    badge.font      = [UIFont fontWithName: fontName size: 15];
    badge.text      = @"10";
    badge.textAlignment = NSTextAlignmentCenter;
    
    badge.layer.cornerRadius = 10.0f;
    badge.layer.backgroundColor = [UIColor yellowColor].CGColor;
    badge.clipsToBounds      = YES;
    
    badge.center = CGPointMake(50, 20);
    
    [chat addSubview: badge];
    
    navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: chat];
    

    祝你今天愉快

    对于Swift 3.0

    结果:

    代码: 您可以尝试以下代码:-


    展示你在实现一个方面所做的努力。写一些代码来检查你的
    viewDidLoad
    方法中是否有未读的消息。如果返回true,则更改按钮的图像,否则保持原样。我有一个代码,可以提供我没有看到的聊天信息的数量。问题是图像中必须包含该数字。这意味着我必须插入大量图像。我试图用self.navigationItem.RightBarButtonim.title=@“2”在按钮上写出数字;但似乎不起作用。嘿@niper007,如何解决这个问题。@niper007 iTroyd23回答也很好我想投票,但我不能因为我对stackoverflow太陌生。我需要更多的声誉BBBadgeBarbuttonItem提供了太多需要定制的属性。UIBarButtonItem+Badge.h UIBarButtonItem+Badge.mAbove链接提供了一个很好的类别来实现这一点。提供了我所需要的。还想补充一点,如果在viewDidLoad方法中使用此类别,则该类别可能无效。-(void)viewdideappease:(BOOL)animated{}方法是您的配置和设置应该使用的方法。我怀疑UIBarbutton不太在那里,直到viewDidAppearThis是我喜欢的(+1)。它在iOS11.4上工作。它有一个问题很容易解决。图标图像应初始化为[[UIImage ImageName:@“image_name”]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate],以便图标继承与显示的任何其他图标相同或默认的UI颜色色调。执行此操作后,我的工具栏按钮图像的色调将不受尊重。我需要设置图像本身的着色颜色,并将图像上的渲染模式设置为“始终使用模板”
    override func viewDidLoad() {
      super.viewDidLoad()
    
      // badge label
      let label = UILabel(frame: CGRect(x: 10, y: -10, width: 20, height: 20))
      label.layer.borderColor = UIColor.clear.cgColor
      label.layer.borderWidth = 2
      label.layer.cornerRadius = label.bounds.size.height / 2
      label.textAlignment = .center
      label.layer.masksToBounds = true
      label.font = UIFont(name: "SanFranciscoText-Light", size: 13)
      label.textColor = .white
      label.backgroundColor = .red
      label.text = "80"
    
      // button
      let rightButton = UIButton(frame: CGRect(x: 0, y: 0, width: 18, height: 16))
      rightButton.setBackgroundImage(UIImage(named: "inbox"), for: .normal)
      rightButton.addTarget(self, action: #selector(rightButtonTouched), for: .touchUpInside)
      rightButton.addSubview(label)
    
      // Bar button item
      let rightBarButtomItem = UIBarButtonItem(customView: rightButton)
    
      navigationItem.rightBarButtonItem = rightBarButtomItem
    }
    
    func rightButtonTouched() {
      print("right button touched")
    }
    
      UILabel *badgeLbl = [[UILabel alloc]initWithFrame:CGRectMake(16, -2, 18, 18)];
    badgeLbl.backgroundColor = [UIColor whiteColor];
    badgeLbl.textColor = [UIColor blackColor];
    badgeLbl.textAlignment = NSTextAlignmentCenter;
    badgeLbl.layer.cornerRadius = 9.0;
    badgeLbl.layer.masksToBounds = true;
    badgeLbl.text = @"10";
    
    UIButton *notificationBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
    [notificationBtn setBackgroundImage:[UIImage imageNamed:@"image_name"] forState:UIControlStateNormal];
    [notificationBtn setTitle:@"" forState:UIControlStateNormal];
    [notificationBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [notificationBtn addSubview:badgeLbl];
    
    UIBarButtonItem *notificationBarBtn = [[UIBarButtonItem alloc]initWithCustomView:notificationBtn];
    self.navigationItem.rightBarButtonItem = notificationBarBtn;