Custom button Settings Badgenumber

Source: Internet
Author: User

TabbarButton.h

@interface*Badgevalueview; -(void) Setitembadgenumber: (nsinteger) number; - (CGRect) Imagerectforcontentrect: (CGRect) contentrect; - (CGRect) Titlerectforcontentrect: (CGRect) contentrect; @end
Tabbarbutton.m

@implementationTabbarbutton- (ID) init{if(self =[Super Init]) {[Self createbadgevalueview]; } returnSelf ;}#pragmaMark-Create badgevalueview-(void) createbadgevalueview{cgfloat x= the-BADGEVALUEVIEWWH +5; CGFloat y= -5; _badgevalueview=[[UIButton alloc] Initwithframe:cgrectmake (x, Y,BADGEVALUEVIEWWH, BADGEVALUEVIEWWH)]; [_badgevalueview setbackgroundimage:[uiimage imagenamed:@"BADGEBG"] Forstate:uicontrolstatenormal]; _badgevalueview.titlelabel.font=kFont12; [_badgevalueview Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal]; _badgevalueview.hidden=YES; _badgevalueview.adjustsimagewhenhighlighted=NO; [Self Addsubview:_badgevalueview];}#pragmaMark-Set the number in the small red circle-(void) Setitembadgenumber: (Nsinteger) number{if(Number! =0) { if(Self.badgeValueView.hidden) {Self.badgeValueView.hidden=NO; } [Self.badgevalueview settitle:[nsstring stringWithFormat:@"%d", number] Forstate: (UIControlStateNormal)]; }Else{Self.badgeValueView.hidden=YES; }}
To invoke a custom button:
    //1. Create a custom buttonTabbarbutton *button =[[Tabbarbutton alloc]init]; Button.tag=index; CGFloat btnx= KSCREENW/4* (Index-Ten) + the; Button.frame= CGRectMake (Btnx,5, $, $); //2. Setting button pictures and events[button Setimage:[uiimage Imagenamed:normal] forstate:uicontrolstatenormal];    [Button Setimage:[uiimage imagenamed:selected] forstate:uicontrolstatedisabled];        [Button addtarget:self action: @selector (Changeviewcontroller:) Forcontrolevents:uicontroleventtouchdown]; //3. Center The picture inside the buttonButton.imageView.contentMode =Uiviewcontentmodecenter; //4. Create the text below the buttonUILabel *label = [[UILabel alloc]initwithframe:cgrectmake (Btnx, -, $, -)]; Label.tag= index +Ten; Label.text=title; Label.font=kFont11; Label.textcolor=[Uicolor Graycolor]; Label.textalignment=Nstextalignmentcenter;
Add to Custom Tabbar [_mytabbar Addsubview:label]; [_mytabbar Addsubview:button];

Set the Badgenumber of the button
[Button setitembadgenumber:1];

Custom button Settings Badgenumber

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.