yhl
yhl

Reputation: 699

How to create an internal notification (think red circle displaying a number)

Many apps have a sort of visual indicator showing when you have "new activity" or "new messages". I am 'not' talking about push notifications (though they appear similar). To better illustrate this, if you use the FB app, the globe icon (notifications) display a red circle with a number which represents how many new messages you may have since you last visited.

I'm wondering how that might be done because the only thing I can think of is to place an image view or text field over the icon that will display a number representing the number of new messages a user may have.

Upvotes: 0

Views: 1831

Answers (1)

Live2Enjoy7
Live2Enjoy7

Reputation: 1105

You have to use a bit of hacking to achieve this because badges in iOS natively can only be added to the tabbar items. There is a custom class called MKNumberBadgeView, which allows you to add it to the Nav Bar.

You would do it in the following way:

MKNumberBadgeView *badge=[[MKNumberBadgeView alloc]initWithFrame:CGRectMake(92, 0, 40, 40)];
badge.value=6;
[self.navigationController.navigationBar addSubview:badge];

a few points: you will have to play around with placement of the badge on the nav bar, so adjust your cgrectmake.

I would suggest making the badge a property because eventually you will want to remove it from the view. In that case, you would call self.badge removeFromSuperview.

This class uses core graphics to draw the badge, looks really nice.

You can find more about it here:

https://www.cocoacontrols.com/controls/mknumberbadgeview

Upvotes: 1

Related Questions