How to add Badges on UIBarbutton item?

前端 未结 10 1740
广开言路
广开言路 2020-11-28 21:29

Hi friends am new to iphone developing. Am struggle with add badge values on UIBarbutton item on right side. I have tried but i can\'t solve this problem. Can anyone help me

10条回答
  •  盖世英雄少女心
    2020-11-28 21:36

    Finally i found the way to add badges on UIBarbutton item. I searched lot but not found the correct answer. So i created UIButton and add it as a Custom view on rightbarbutton item. Add add the MKNumberBadgeView for display the badge number. Below i have add my code for you.

    // Initialize NKNumberBadgeView...
    MKNumberBadgeView *number = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(60, 00, 30,20)];
    number.value = 10;
    
    // Allocate UIButton
    UIButton *btn = [UIButton  buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 70, 30);
        btn.layer.cornerRadius = 8;
        [btn setTitle:@"Button" forState:UIControlStateNormal];
        [btn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
        //[btn setBackgroundColor:[UIColor blueColor]];
        [btn setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.1 alpha:0.2]];
        btn.font = [UIFont systemFontOfSize:13];
        //[btn setFont:[UIFont systemFontOfSize:13]];
        [btn addSubview:number]; //Add NKNumberBadgeView as a subview on UIButton
    
    // Initialize UIBarbuttonitem...
    UIBarButtonItem *proe = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = proe;
    

    Thanks.

提交回复
热议问题