在UINavigationbar中显示计数器等通知徽章 [英] Displaying notification badge like counter in UINavigationbar
问题描述
我要求在iPhone导航栏中显示待处理通知的数量。外观应该与通知徽章的外观相似 - 但这些不是APNS通知。它们是从具有类似目的的私人服务器发送的。
I have a requirement to display number of pending notifications in iPhone navigation bar. The appearance should be like that of notification badge - but these are not APNS notifications. They are the ones sent from private server with similar purpose.
我尝试添加右/左按钮( UIBarButtonItem
)在我的 UINavigationbar
中,但它看起来非常严格。我无法设置其宽度,字体等。请参阅我的代码:
I tried adding a right/left button (UIBarButtonItem
) in my UINavigationbar
but it seems like it is very rigid in appearance. I can't set its width, fonts etc. See my code:
self.notifButton = [[UIBarButtonItem alloc] initWithTitle:@"0" style:
UIBarButtonItemStyleBordered target:self action:@selector(TouchNotif)];
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:self.notifButton];
self.navigationItem.rightBarButtonItems = items;
由于项目数组中还添加了其他2项,因此导航栏会混乱。他们的字体,宽度等我不能玩,或者我不知道应该怎么创建它们。
Because of other 2 items also added to items array, navbar is cluttered. Their fonts, width etc I cannot play with, or maybe I don't know how should I create them.
我的问题:
1)在导航栏右侧区域内容纳至少3个项目的正确方法是什么?我问这个是因为我找不到一种方法来玩我使用的 UIButtons
的宽度和字体。
1) What is proper way to accommodate at least 3 items in navbar right area? I am asking this because I don't find a way to play with width and font of the UIButtons
I use.
2)如果我想为我的通知按钮设置自定义外观(就像通知徽章一样) - 是否有任何指示我如何制作它?要使用哪个控件,如何设置其框架和字体将在 UINavigationBar
中设置?
2) If I want to have custom appearance for my notification button (just like notification badge) - are there any pointers how do I make it? Which control to use, how to set its frame and font which will be allowed within UINavigationBar
?
请帮忙。
推荐答案
您需要创建一个包含自定义视图的 UIBarButtonItem
initWithCustomView
。
自定义视图可以是自定义UIButton,其中数字徽章为子视图。使用此自定义视图,您还可以控制按钮的宽度。
You need to create a UIBarButtonItem
that contains a custom view using initWithCustomView
.
The custom view could be a custom UIButton with a number badge as subview. With this custom view you can also control the width of the buttons.
没有公共API可以直接创建通知徽章。如果是标签栏项目,您可以使用属性 badgeValue
设置徽章,但不能使用 UIBarButtonItem
。
在这里,你需要使用这个开源控件: MKNumberBadgeView 。
There is no public API to create a notification badge directly. In case of a tab bar item you could set a badge using the property badgeValue
- but not with UIBarButtonItem
.
Here you need to use this open source control: MKNumberBadgeView.
请注意,自iOS 5起,属性 rightBarButtonItems
可用。
如果您只需要一个项目集改为 rightBarButtonItem
。
Note that the property rightBarButtonItems
is available since iOS 5.
If you only need one item set the rightBarButtonItem
instead.
这篇关于在UINavigationbar中显示计数器等通知徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!