Ionic tabs Badge

2019-06-16 00:40发布

问题:

I am trying to add a badge on my Icons in the tabs. The current result is: http://play.ionic.io/app/decfc14cb171

Does anyone know how to put them in the top-right corner of each icon?

I tried using but it proved more problematic in other aspects, although with the "badge" attribute is was easier to achieve the desired effect. Is there a way to replicate it without using ion-tabs?

回答1:

[ works only for ioinc 1 ]

See this: http://play.ionic.io/app/52586f24b84d
You need to make a class with relative position

.badge-container{
      position: relative;
}

And assign it to <i> tag this way, badge will be automatically adjusted

<i class="icon ion-home badge-container"><span class="badge badge-assertive">3</span></i>

Same goes for other tab

<i class="icon ion-ios-paper badge-container"><span class="badge badge-assertive">5</span></i>

Now you can change further position of badge also by giving margin etc to span with badge.



回答2:

I'd advise using Ionic's ion-tabs directive, as it has "first class" support for badges. The ion-tab element has a "badge" attribute which makes it really easy to add text (in your case a number) to an icon.

I've written a demo of it in action here:

http://play.ionic.io/app/c6e96276e8fd

The code to add the tags is here:

<ion-tabs class="tabs-icon-top tabs-striped">

 <ion-tab title="Home" icon="ion-home" href="#/tab/home" badge="3" badge style="badge-assertive">
   <ion-nav-view name="home-tab"></ion-nav-view>
 </ion-tab>

 <ion-tab title="About" icon=" ion-ios-paper" href="#/tab/about" badge="5" badge-style="badge-assertive">
   <ion-nav-view name="about-tab"></ion-nav-view>
 </ion-tab>

</ion-tabs>

And the result looks like this:



回答3:

In the latest Ionic doc appears a different way of doing this:

Using the tabBadget and tabBadgetStyle attributes in the tab code, like this:

 <ion-tabs>
      <ion-tab tabIcon="call" [root]="tabOne" tabBadge="3" tabBadgeStyle="danger"></ion-tab>
      <ion-tab tabIcon="chatbubbles" [root]="tabTwo" tabBadge="14" tabBadgeStyle="danger"></ion-tab>
      <ion-tab tabIcon="musical-notes" [root]="tabThree"></ion-tab>
    </ion-tabs>

where it says "danger" is the color that you define in the Theme...

Hope it helps the new ones coming to find answer in 2017!