回答
最后我找到了在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;
谢谢。
这很简单,最好的方法!
MKNumberBadgeView *numberBadge = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(230, -51, 40, 40)];
numberBadge.value = 5;
self.navigationController.navigationBar.layer.zPosition = -1;
[self.view addSubview:numberBadge];
我做类似的东西MaxMa,但我只是说干就干,直接添加徽章到self.navigationController.navigationBar。
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];
}
如何在viewWillDisappear中将其删除?你可以添加该代码吗? – daniel 2012-11-30 20:32:17
当然,我添加了这个示例来展示如何在视图消失时将其删除。 – 2012-12-04 20:25:11
我知道这已经被解决时将其删除,但我想我可能会添加什么,我发现这个答案的完整性的缘故。
您也可以直接将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>
的看法。您还需要随身携带徽章的框架才能使其显示在正确的位置。
这样你不必从导航栏中删除/添加视图。
我知道这篇文章很老,但与iOS7 MKNumberBadgeView的外观并不真正匹配标签栏项徽章设计。 我发现这herits的UIBarButtonItem这个其他组件和做的工作非常好:
https://github.com/TanguyAladenise/BBBadgeBarButtonItem
希望这可以帮助其他iOS7开发者喜欢我
phyzalis有一个很好的答案,还有的分类版本他的解决方案在这里:
这里是你如何使用它:
// 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";
完美答案! – hyd00 2016-04-06 17:14:59
我得到了“属性”badgeValue“在UIBarButtonItem上找不到”的问题? – 2016-10-05 20:32:34
不适用于iOS 11和xcode 9 .. – 2017-11-15 09:58:18
更新了夫特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必须写你的代码按你的决定你的逻辑,即如何保持数据库徽章计数增加你的徽章。
享受..!
搜索太多的解决方案后,我发现了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
希望进展!
- 1. 如何在像UITabBarController这样的UIBarbutton上添加徽章?
- 2. 如何添加TabBar徽章?
- 3. android在Expandablelistview上添加徽章
- 4. 在viewpager上添加通知徽章数
- 5. 如何添加徽章列出
- 6. 如何在工具栏上添加徽章MenuItem图标
- 7. 如何在启动图标上添加未读徽章数?
- 8. 如何在Font Awesome符号上添加徽章?
- 9. 向Trello添加徽章
- 10. 如何更改UIBarbutton中的徽章背景颜色?
- 11. 在标签页中添加徽章
- 12. 如何在提交时增加徽章?
- 13. 如何在UIBarButton项目下添加UIImage项目
- 14. 如何在shields.io徽章中使用“徽标”选项?
- 15. 如何在应用中添加通知徽章?
- 16. 如何在tabhost中添加徽章计数器android
- 17. ionic 2如何在选项卡中增加徽章
- 18. 如何通过钛加速器将徽章添加到操作栏菜单项?
- 19. Android - 在MenuItems上画徽章
- 20. TabBarItem上的徽章
- 21. 在Windows/OSX/Linux上向Python中的图标添加徽章
- 22. 在android的应用程序图标上添加通知徽章
- 23. 在分段控件上添加自定义徽章
- 24. 在离子3中的图标上添加徽章
- 25. 谷歌地图iOS SDK - 添加徽章
- 26. 通过shields.io徽章添加SonarQube覆盖
- 27. 添加谷歌信任商店徽章
- 28. UINavigationController并向右侧添加徽章BarButtonItem
- 29. 的iOS - 添加徽章的UITableViewCell
- 30. 将Google信任徽章添加到Magento
此代码的结果创建一个非常丑陋的按钮(但它确实有效)。你已被警告;) – Ralphleon 2012-02-20 19:06:16
这对iOS 5更好,因为你可以更好地控制按钮的外观。否则,最终会在导航栏中出现难看的按钮。 – Quentamia 2012-05-10 15:28:50