2011-04-16 83 views

回答

29

最后我找到了在UIBarbutton项目上添加徽章的方法。我搜查了很多,但没有找到正确的答案。所以我创建了UIButton并将其作为右侧栏按钮上的自定义视图添加。添加添加MKNumberBadgeView用于显示徽章号码。下面我为你添加我的代码。

// 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; 

谢谢。

+0

此代码的结果创建一个非常丑陋的按钮(但它确实有效)。你已被警告;) – Ralphleon 2012-02-20 19:06:16

+0

这对iOS 5更好,因为你可以更好地控制按钮的外观。否则,最终会在导航栏中出现难看的按钮。 – Quentamia 2012-05-10 15:28:50

7

这很简单,最好的方法!

enter image description here

MKNumberBadgeView *numberBadge = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(230, -51, 40, 40)]; 
numberBadge.value = 5; 

self.navigationController.navigationBar.layer.zPosition = -1; 
[self.view addSubview:numberBadge]; 
12

我做类似的东西MaxMa,但我只是说干就干,直接添加徽章到self.navigationController.navigationBar。

enter image description here

MKNumberBadgeView *numberBadge = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(35, 0, 40, 40)]; 
numberBadge.value = 1; 

[self.navigationController.navigationBar addSubview:numberBadge]; 

只要确保viewWillDisappear期间从子视图中取出,并viewDidAppear过程中添加了回去。它仍然看起来有点冒险,但我更喜欢这个黑客,然后改变导航栏z顺序。

要viewWillDisappear

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    [numberBadge removeFromSuperview]; 
} 
+0

如何在viewWillDisappear中将其删除?你可以添加该代码吗? – daniel 2012-11-30 20:32:17

+1

当然,我添加了这个示例来展示如何在视图消失时将其删除。 – 2012-12-04 20:25:11

0

我知道这已经被解决时将其删除,但我想我可能会添加什么,我发现这个答案的完整性的缘故。

您也可以直接将MKNumberBadgeView直接添加到UIBarButtonItem的视图中。使用MonoTouch的(C#),这是你如何让我相信它很容易将其转换为对象 - 为UIBarButtonItem

 //barbutton is some UIBarButtonItem. Make sure to check for view. In 
     //ViewDidLoad(), the view for the barbutton might not exist yet. 
     Selector sel = new Selector("view"); 
     var handle = Messaging.intptr_objc_msgSend(barbutton.Handle, sel.Handle); 
     var view = Runtime.GetNSObject(handle) as UIView; 
     var mkBadge = ... //the badge 
     view.Add(badge); 
     view.Layer.ZPosition = <some large number> 

的看法。您还需要随身携带徽章的框架才能使其显示在正确的位置。

这样你不必从导航栏中删除/添加视图。

18

phyzalis有一个很好的答案,还有的分类版本他的解决方案在这里:

UIBarButtonItem+Badge

这里是你如何使用它:

// Build your regular UIBarButtonItem with Custom View 
UIImage *image = [UIImage imageNamed:@"someImage"]; 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0,0,image.size.width, image.size.height); 
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown]; 
[button setBackgroundImage:image forState:UIControlStateNormal]; 

// Make BarButton Item 
UIBarButtonItem *navLeftButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 
self.navigationItem.leftBarButtonItem = navLeftButton; 

// this is the key entry to change the badgeValue 
self.navigationItem.leftBarButtonItem.badgeValue = @"1"; 
+0

完美答案! – hyd00 2016-04-06 17:14:59

+0

我得到了“属性”badgeValue“在UIBarButtonItem上找不到”的问题? – 2016-10-05 20:32:34

+1

不适用于iOS 11和xcode 9 .. – 2017-11-15 09:58:18

0

更新了夫特3:下面简单的代码

使用添加上的UIBarButtonItem徽章;

// Variable Declartion 

var badgeCount = Int() 

// Instance Method 

    func setUpBadgeCountAndBarButton() { 
     // badge label 
     let label = UILabel(frame: CGRect(x: 10, y: -05, width: 25, height: 25)) 
     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.textColor = .white 
     label.font = label.font.withSize(12) 
     label.backgroundColor = .red 
     label.text = "\(self.badgeCount)" 

     // button 
     let rightButton = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 35)) 
     rightButton.setBackgroundImage(UIImage(named: "notification_dash"), for: .normal) 
     rightButton.addTarget(self, action: #selector(notificationBarButtonClick), for: .touchUpInside) 
     rightButton.addSubview(label) 

     // Bar button item 
     let rightBarButtomItem = UIBarButtonItem(customView: rightButton) 
     navigationItem.rightBarButtonItem = rightBarButtomItem 
    } 

// Call To Method 

self.badgeCount = 11 
self.setUpBadgeCountAndBarButton() 

//注:根据您收到notification.You必须写你的代码按你的决定你的逻辑,即如何保持数据库徽章计数增加你的徽章。

享受..!

0

搜索太多的解决方案后,我发现了Objective-C的

转到下面的链接这一最佳解决方案和下载两个文件“的UIBarButtonItem + Badge.h”“的UIBarButtonItem + Badge.m”,并添加到您的项目:

https://github.com/mikeMTOL/UIBarButtonItem-Badge

然后导入类:

#import "UIBarButtonItem+Badge.h" 

,并记下以下行添加徽章:

self.navigationItem.rightBarButtonItem.badgeValue = @"1"; //your value 

希望进展!

相关问题