2011-04-16 84 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 

希望進展!

相關問題