如何在 UIBarbutton 项目上添加徽章?
嗨,朋友们,我是 iPhone 开发新手。我正在努力在右侧的 UIBarbutton 项目上添加徽章值。我已经尝试过,但无法解决这个问题。谁能帮助我。
提前致谢!
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.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我知道这篇文章已经很老了,但是在 iOS7 中,MKNumberBadgeView 的外观与选项卡栏项目徽章设计并不真正匹配。
我发现这个继承 UIBarButtonItem 并很好地完成工作的另一个组件:
https://github.com/TanguyAladenise/BBBadgeBarButtonItem
希望这可能会帮助像我这样的其他 iOS7 开发者
I know this post is pretty old but with iOS7, MKNumberBadgeView's appearance does not really match the tab bar item badge design.
I have found this other component which herits UIBarButtonItem and do the job very well :
https://github.com/TanguyAladenise/BBBadgeBarButtonItem
Hope this may help other iOS7 developers like me
最后我找到了在 UIBarbutton 项目上添加徽章的方法。我搜索了很多但没有找到正确的答案。所以我创建了 UIButton 并将其添加为 rightbarbutton 项目上的自定义视图。添加 MKNumberBadgeView 用于显示徽章编号。下面我为您添加了我的代码。
谢谢。
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.
Thanks.
phyzalis 有一个很好的答案,这里有他的解决方案的分类版本:
UIBarButtonItem+Badge
下面是如何使用它:
phyzalis has a good answer, there's a categorized version of his solution here:
UIBarButtonItem+Badge
Here's how you can use it:
我做了与 MaxMa 类似的操作,但我只是继续将徽章直接添加到 self.navigationController.navigationBar 中。
只需确保在 viewWillDisappear 期间将其从子视图中删除,并在 viewDidAppear 期间将其添加回来。它看起来仍然有点 hacky,但我对这个 hack 比更改导航栏 z 顺序更舒服。
在 viewWillDisappear 期间将其删除
I did something similar to MaxMa, but I just went ahead and added the badge directly to the self.navigationController.navigationBar.
Just make sure to remove it from the subview during viewWillDisappear and add it back during viewDidAppear. It still seems a little hacky, but I'm more comfortable with this hack then changing the nav bar z-order.
To remove it during viewWillDisappear
这很简单,也是最好的方法!
It's simple and the best way !
使用下面的简单代码在 UIBarButtonItem 上添加徽章;
//注意:根据您收到的通知增加您的徽章。您必须根据您决定的逻辑编写代码,即如何在数据库中维护该徽章计数。
享受..!
use below simple code to add the badge on UIBarButtonItem;
//Note: Increase your badge as per you received notification.You have to write your code as per your decided your logic i.e. how to maintain that badge count number in database.
Enjoy..!
我知道这个问题已经解决了,但为了完整起见,我想我可以将我发现的内容添加到这个答案中。
您还可以直接将
MKNumberBadgeView
添加到UIBarButtonItem
的视图中。使用 Monotouch (C#),这就是获取UIBarButtonItem
视图的方法,我确信将其转换为 Obj-C 很容易。您还需要调整徽章的框架,使其显示在正确的位置。
这样您就不必从导航栏中删除/添加视图。
I know this has been solved already,but I thought I might add what I have discovered to this answer for the sake of completeness.
You can also just add
MKNumberBadgeView
directly to the view for theUIBarButtonItem
. Using Monotouch (C#), this is how you get the view for theUIBarButtonItem
I'm sure it's easy to convert this to Obj-C. You will also need to play around with the Frame for the badge to get it to show up in the right place.
This way you wont have to remove/add the view from the navigationbar.
在搜索了太多解决方案后,我找到了 Objective-C 的最佳解决方案
转到以下链接并下载两个文件 "UIBarButtonItem+Badge.h" 和 "UIBarButtonItem+Badge.m"并添加到您的项目中:
https://github.com/mikeMTOL/UIBarButtonItem-Badge
然后在你的类中导入:
并写下以下行来添加徽章:
希望它能工作!
After searching too many solutions I found this best solution for Objective-C
Goto Following Link and download two files "UIBarButtonItem+Badge.h" and "UIBarButtonItem+Badge.m" and add to your project :
https://github.com/mikeMTOL/UIBarButtonItem-Badge
Then import in your class :
And write down following line to add badge :
Hope it will Work !!!
这是一个简单的 Swift 4 解决方案(带有一些自定义) https://github.com/Syngmaster/BadgedBarButtonItem< /a>
只需将类拖放到您的项目中,您就可以像这样使用它:
Here is a simple Swift 4 solution for it (with some customisation) https://github.com/Syngmaster/BadgedBarButtonItem
Just drag and drop the class into your project and you can use it like that:
添加 UIActivityIndicatorView 而不替换 UIBarButtonItem 的扩展。
Extension to add an UIActivityIndicatorView without replacing the UIBarButtonItem.