A project needs to show a little red dot on the Uitabbar, where three methods are collected.

Source: Internet
Author: User
Tags vars

1. Use the system comes with, and can display the number on the Little red dot.

[ItemOne Setbadgevalue:@ ""// show Little Red dots without numbers [itemone setbadgevalue:@ "1 "]; // show Little Red dots with numbers

The above shortcomings: Little red dot too big, hurt Ah!

2. Use the picture, create the picture, set the rendering of the picture.

UIImage * Normalimage = [[UIImage imagenamed:@"Pic_msg_yes_nor"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; UIImage* Selectimage = [[UIImage imagenamed:@"Pic_msg_yes_sel"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; Uitabbaritem*itemone=[[uitabbaritem Alloc]initwithtitle:@"message"image:normalimage Selectedimage:selectimage];/** Set the rendering mode of UIImage: Uiimage.renderingmode Coloring (Tint Color) is one of the iOS7 interfaces. Set the rendering mode of the UIImage: Uiimage.renderingmode A significant change, you can set a UIImage whether to use the tint Color of the current view when rendering. UIImage added a read-only property: Renderingmode, corresponding to a new method: Imagewithrenderingmode:, It uses the Uiimagerenderingmode enumeration value to set the Renderingmode property of the picture.  The enumeration contains the following values: Uiimagerenderingmodeautomatic//automatically adjusts the rendering mode based on the environment in which the picture is used and the drawing context in which it is located.  Uiimagerenderingmodealwaysoriginal//Always draws the original state of the picture without using tint Color.   Uiimagerenderingmodealwaystemplate//Always draws a picture based on tint color, ignoring the color information of the picture. The default value for the Renderingmode property is Uiimagerenderingmodeautomatic*/

Disadvantage: This will only show the small red dots, can not display the specific number. But the landlord of the project does not need to display the numbers, the use of this. Convenient and concise.

3. Using Catgory, expand Uitabbar

This method is reproduced from: http://blog.csdn.net/lilinoscar/article/details/47103747

The first step is to build a uitabbar category.

The second step is to write the code.

. h file

[OBJC]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. @interface Uitabbar (Badge)
    3. -(void) Showbadgeonitemindex: (int) index; //show Little red dot
    4. -(void) Hidebadgeonitemindex: (int) index; //Hide Little Red dot
    5. @end


. m file

[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. #import "Uitabbar+badge.h"
    2. #define The number of Tabbaritemnums 4.0//tabbar if 5 is set to 5.0
    3. @implementation Uitabbar (Badge)
[OBJC]View Plaincopy
  1. Show Little Red Dot
  2. -(void) Showbadgeonitemindex: (int) index{
  3. //Remove the Little red dot before
  4. [self removebadgeonitemindex:index];
  5. //New Little Red dot
  6. UIView *badgeview = [[UIView alloc]init];
  7. Badgeview. Tag = 888 + index;
  8. Badgeview. Layer. Cornerradius = 5; Round
  9. Badgeview. backgroundcolor = [Uicolor redcolor]; Color: Red
  10. CGRect tabframe = self. frame;
  11. //Determine the location of the Little red dot
  12. float Percentx = (index +0. 6)/tabbaritemnums;
  13. CGFloat x = CEILF (percentx * tabframe. Size. width);
  14. CGFloat y = Ceilf (0. 1 * tabframe. Size. height);
  15. Badgeview. frame = CGRectMake (x, y, 10, 10); Round size is ten
  16. [self addsubview:badgeview];
  17. }
[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. Hide the Little Red dot
    2. -(void) Hidebadgeonitemindex: (int) index{
    3. //Remove the Little red dot
    4. [self removebadgeonitemindex:index];
    5. }
[OBJC]View Plaincopy
  1. Remove the Little red dot
  2. -(void) Removebadgeonitemindex: (int) index{
  3. //Remove according to tag value
  4. For (UIView *subview in self. subviews) {
  5. if (subView. Tag = = 888+index) {
  6. [SubView Removefromsuperview];
  7. }
  8. }
  9. }
  10. @end

The third step is to introduce the classes that need to be used.

[OBJC]View Plaincopy
    1. #import "Uitabbar+badge.h"

The reference code is as follows:

[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
      1. Show
      2. [self. Tabbarcontroller. TabBar Showbadgeonitemindex:2];
      3. Hide
      4. [self. Tabbarcontroller. TabBar Hidebadgeonitemindex:2]

A project needs to show a little red dot on the Uitabbar, where three methods are collected.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.