Customized tabbar solution 2

Source: Internet
Author: User

Custom tabbar can solve the problem that custom images cannot be displayed (only one color is displayed). Custom tabbar must inherit the uitabbarcontroller

Original Image:

(1), 650) This. width = 650; "src =" http://img.blog.csdn.net/20141010214007546 "/> (2), 650) This. width = 650; "src =" http://img.blog.csdn.net/20141010214352826 "/> (3), 650) This. width = 650; "src =" http://img.blog.csdn.net/20141010214255562 "/> (4), 650) This. width = 650; "src =" http://img.blog.csdn.net/20141010214533649 "/> (5) 650) This. width = 650; "src =" http://img.blog.csdn.net/20141010214329484 "/>

The following is a custom tabbar. Each button can display an image (this prevents the image from displaying only one color)

:


650) This. width = 650; "src =" http://img.blog.csdn.net/20141010213726757 "/>


Code implementation:

First, create a class customtabbar that inherits uibutton,

In the. h file:

// Specify the Protocol to display the corresponding page by clicking the button, just like the system tabbar

@ Protocol customtabbardelegate <nsobject>

-(Void) didselectbaritematindex :( nsinteger) index;

@ End

@ Interface customtabbar: uibutton
@ Property (nonatomic, assign) nsinteger index;
@ Property (nonatomic, retain) ID <mtmtabbardelegate> delegate;
-(ID) initwithframe :( cgrect) frame withimage :( uiimage *) image andselectedimage :( uiimage *) image;
@ End



In the. M file


# Import "customtabbar. H"

@ Interface customtabbar ()

@ End
@ Implementation customtabbar

-(ID) initwithframe :( cgrect) Frame
{
Self = [Super initwithframe: frame];
If (Self ){
// Initialization code

}
Return self;
}

-(ID) initwithframe :( cgrect) frame withimage :( uiimage *) image andselectedimage :( uiimage *) selectedimage {
Self = [Super initwithframe: frame];
If (Self ){
Self. Frame = frame;
[Self setadjustsimagewhenhighlighted: No];
[Self setimage: Image forstate: uicontrolstatenormal];
[Self setimage: selectedimage forstate: uicontrolstateselected];
[Self addtarget: Self action: @ selector (Click :) forcontrolevents: uicontroleventtouchupinside];
}
Return self;
}

-(Void) Click :( uibutton *) BTN {
If ([self. Delegate respondstoselector: @ selector (didselectbaritematindex :)]) {
[Self. Delegate didselectbaritematindex: Self. Index];
}
Switch (INT) (BTN. Selected )){
Case 0:
BTN. Selected = yes;
Break;
Case 1:
BTN. Selected = no;
Break;
Default:
Break;
}
}


@ End


2. Create a class customizedtabbarcontroller to inherit uitabbarcontroller


In the. M file

-(Void) loadviewcontroller
{
Covercollectionviewcontroller * covervc = [[covercollectionviewcontroller alloc] init];
// Covervc. Title = @ "Daily cover ";
Customizednavigationcontroller * covernavc = [[customizednavigationcontroller alloc] initwithrootviewcontroller: covervc];
Covernavc. navigationitem. Title = @ "idailywatch ";

Newstableviewcontroller * newsvc = [[newstableviewcontroller alloc] init];
// Newsvc. Title = @ "watch Magazine ";
Customizednavigationcontroller * newsnavc = [[customizednavigationcontroller alloc] initwithrootviewcontroller: newsvc];

Brandstableviewcontroller * brandsvc = [[brandstableviewcontroller alloc] init];
// Brandsvc. Title = @ "brand wiki ";
Customizednavigationcontroller * brandsnavc = [[customizednavigationcontroller alloc] initwithrootviewcontroller: brandsvc];

Videoscollectionviewcontroller * videosvc = [[videoscollectionviewcontroller alloc] init];
// Videosvc. Title = @ "video ";
Customizednavigationcontroller * videosnavc = [[customizednavigationcontroller alloc] initwithrootviewcontroller: videosvc];

Moreviewcontroller * morevc = [[moreviewcontroller alloc] init];
// Morevc. Title = @ "more ";
Customizednavigationcontroller * morenavc = [[customizednavigationcontroller alloc] initwithrootviewcontroller: morevc];

Self. viewcontrollers = @ [covernavc, newsnavc, brandsnavc, videosnavc, morenavc];
Self. selectedindex = 0;
Release_safe (covernavc );
Release_safe (covervc );
Release_safe (brandsnavc );
Release_safe (brandsvc );
Release_safe (videosnavc );
Release_safe (videosvc );
Release_safe (morenavc );
Release_safe (morevc );
}


-(Void) setuptabbar
{
For (INT I = 0; I <5; I ++ ){
If (I <3 ){
Customtabbar * bar = [[customtabbar alloc] initwithframe: cgrectmake (0 + 80 * I, 0, 80, 49) withimage: [uiimage imagenamed: [nsstring stringwithformat: @ "% d", I + 100] andselectedimage: [uiimage imagenamed: [nsstring stringwithformat: @ "% d", I + 130];
Bar. Index = I;
Bar. Delegate = self;
Bar. backgroundcolor = RGB (245,245,245 );
[Self. tabbar addsubview: bar];
[Bar release];
If (I = 0 ){
Bar. Selected = yes;
}
} Else {
Customtabbar * bar = [[customtabbar alloc] initwithframe: cgrectmake (0 + 40 * (I + 3), 0, 40, 49) withimage: [uiimage imagenamed: [nsstring stringwithformat: @ "% d", I + 100] andselectedimage: [uiimage imagenamed: [nsstring stringwithformat: @ "% d", I + 130];
Bar. Index = I;
Bar. Delegate = self;
Bar. backgroundcolor = RGB (245,245,245 );
[Self. tabbar addsubview: bar];
[Bar release];
}
}
}


-(Void) didselectbaritematindex :( nsinteger) index {
[Self resignbatstate];
Self. selectedviewcontroller = self. viewcontrollers [Index];
}
-(Void) resignbatstate {
Nsarray * arr = [self. tabbar subviews];
For (customtabbar * bar in ARR ){
If ([Bar iskindofclass: [mtmtabbar class]) {
Bar. Selected = no;
}
}
}

-(Void) changeviewcontroller :( uibutton *) button
{
Self. selectedindex = button. Tag;
}

-(Void) didreceivememorywarning
{
[Super didreceivememorywarning];
// Dispose of any resources that can be recreated.
}









Customized tabbar solution 2

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.