Two ways to customize the label Toolbar

Source: Internet
Author: User

Set Root View Controller in appdelegate. m

Maintabbarcontroller. h

@interface MainTabbarController : UITabBarController{    UIImageView *_selectedImg;    UIView *_tabbarView;}- (void)showtabbar:(BOOL)show;

Maintabbarcontroller. m

# Import "maintabbarcontroller. H "# import" basenavgationcontroller. H "# import" homeviewcontroller. H "# import" squleviewcontroller. H "# import" searchviewcontroller. H "# import" commentviewcontroller. H "# import" messageviewcontroller. H "@ interface maintabbarcontroller () @ end @ implementation maintabbarcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; // create a level-3 controller [self _ initviewctrl]; // customize the label toolbar // [self _ inittabbarview]; [self _ newinittabarview];} // create a third-level controller-(void) _ initviewctrl {// create a first-level view controller homeviewcontroller * homectrl = [[homeviewcontroller alloc] init]; squleviewcontroller * squlectrl = [[squleviewcontroller alloc] init]; searchviewcontroller * searchctrl = [[searchviewcontroller alloc] init]; commentviewcontroller * commentctrl = [[commentviewcontroller alloc] init]; messageviewcontroller * messagectrl = [[messageviewcontroller alloc] init]; nsarray * viewctrls = @ [homectrl, squlectrl, searchctrl, commentctrl, messagectrl]; // create a second-level navigation Controller/* basenavgationcontroller * navctrl1 = [basenavgationcontroller alloc] handler: homectrl]; optional * navctrl2 = [[basenavgationcontroller alloc] direction: squlectrl]; basenavgationcontroller * navctrl3 = [[using alloc] direction: searchctrl]; optional * navctrl4 = [[basenavgationcontroller alloc] direction: commentctrl]; basenavgationcontroller * navctrl5 = [[basenavgationcontroller alloc] Reverse: messagectrl]; * // create nsmutablearray * navctrls = [[nsmutablearray alloc] limit: 5]; for (INT I = 0; I <5; I ++) {uiviewcontroller * viewctrl = viewctrls [I]; basenavgationcontroller * navctrl = [[basenavgationcontroller alloc] initwithrootviewcontroller: viewctrl]; // Add the Controller to the array [navctrls addobject: navctrl];} // store the navigation controller in the array // nsarray * navctrls = @ [navctrl1, navctrl2, navctrl3, navctrl4, navctrl5]; // create a third-level label controller self. viewcontrollers = navctrls;} // custom toolbar-(void) _ inittabbarview {// 1. hide the toolbar self generated by default. tabbar. hidden = yes; // 2. create a toolbar VIEW _ tabbarview = [[uiview alloc] initwithframe: cgrectmake (0,480-49,320, 49)]; _ tabbarview. backgroundcolor = [uicolor colorwithpatternimage: [uiimage imagenamed: @ "navbg"]; [self. view addsubview: _ tabbarview]; // 3. add the subview button for (INT I = 0; I <5; I ++) {// set the image name nsstring * name = [nsstring stringwithformat: @ "% d", I + 1]; uibutton * button = [uibutton buttonwithtype: uibuttontypecustom]; [Button setimage: [uiimage imagenamed: Name] forstate: uicontrolstatenormal]; button. tag = I; button. frame = cgrectmake (64-42)/2 + 64 * I, (49-44)/2, 42, 44); [Button addtarget: Self action: @ selector (buttonaction :) forcontrolevents: uicontroleventtouchupinside]; [_ tabbarview addsubview: button];} // 4. create selected image _ selectedimg = [[uiimageview alloc] initwithframe: cgrectmake (64-53)/2, (49-45)/2, 53, 45)]; _ selectedimg. image = [uiimage imagenamed: @ "selected"]; [_ tabbarview addsubview: _ selectedimg];} // 2. method 2: retain the custom toolbar-(void) _ newinittabarview {<strong> <span style = "color: # cc0000;"> // 1. remove all buttons on the toolbar </span> </strong> // obtain the subview nsarray * subviews = [self. tabbar subviews]; // nslog (@ "----- subviews: % @", subviews); // remove the uitabbarbutton object for (uiview * view in subviews) {// remove an object of the uitabbarbutton type // convert the uitabbarbutton to the class name class = nsclassfromstring (@ "uitabbarbutton"); // remove the view if ([view iskindofclass: class]) {[view removefromsuperview] ;}// 2. set the background color of tabbarview [self. tabbar setbackgroundimage: [uiimage imagenamed: @ "navbg"]; // 3. add the subview button for (INT I = 0; I <5; I ++) {// set the image name nsstring * name = [nsstring stringwithformat: @ "% d", I + 1]; uibutton * button = [uibutton buttonwithtype: uibuttontypecustom]; [Button setimage: [uiimage imagenamed: Name] forstate: uicontrolstatenormal]; button. tag = I; button. frame = cgrectmake (64-42)/2 + 64 * I, (49-44)/2, 42, 44); [Button addtarget: Self action: @ selector (buttonaction :) forcontrolevents: uicontroleventtouchupinside]; [self. tabbar addsubview: button];} // 4. create selected image _ selectedimg = [[uiimageview alloc] initwithframe: cgrectmake (64-53)/2, (49-45)/2, 53, 45)]; _ selectedimg. image = [uiimage imagenamed: @ "selected"]; [self. tabbar addsubview: _ selectedimg];}-(void) buttonaction :( uibutton *) button {// switch the attempt to self. selectedindex = button. tag; [uiview beginanimations: NULL context: Nil]; [uiview setanimationduration :. 3]; _ selectedimg. center = button. center; // close the animation [uiview commitanimations];} // whether to display the label toolbar-(void) showtabbar :( bool) show {// _ tabbarview. frame. origin. X =-320; error cgrect frame = _ tabbarview. frame; If (show) {// display frame. origin. X = 0;} else {frame. origin. X =-320;} [uiview beginanimations: Nil context: Nil]; [uiview setanimationduration :. 35]; _ tabbarview. frame = frame; [uiview commitanimations];} @ end

Create a navigation controller parent class to facilitate unified navigation bar settings

Basenavgationcontroller. m

- (void)viewDidLoad{    [super viewDidLoad];    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal1"] forBarMetrics:UIBarMetricsDefault];    }
Homeviewcontroller. m

# Import "homeviewcontroller. H "# import" detailviewcontroller. H "# import" maintabbarcontroller. H "@ interface homeviewcontroller () @ end @ implementation homeviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization self. title = @ "personal Center";} return self;}-(void) viewdidload {[Super viewdidload]; // create a button uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect]; button. backgroundcolor = [uicolor greencolor]; button. frame = cgrectmake (90, 90, 90, 50); [Button addtarget: Self action: @ selector (buttonaction) forcontrolevents: uicontroleventtouchupinside]; [self. view addsubview: button];}-(void) buttonaction {detailviewcontroller * detailctrl = [[detailviewcontroller alloc] init]; [self. navigationcontroller pushviewcontroller: detailctrl animated: Yes]; // hide the toolbar maintabbarcontroller * mainctrl = (maintabbarcontroller *) self. tabbarcontroller; [mainctrl showtabbar: No];} @ end
Detailviewcontroller. m

# Import "detailviewcontroller. H "# import" maintabbarcontroller. H "@ interface detailviewcontroller () @ end @ implementation detailviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// when the view is pushed, remove the label bar self. hidesbottombarwhenpushed = yes;} return self;}-(void) viewdidload {[Super viewdidload]; self. title = @ "details"; self. view. backgroundcolor = [uicolor redcolor];}-(void) viewwilldisappear :( bool) animated {[Super viewwilldisappear: animated]; // note: view Controller can get the label toolbar maintabbarcontroller * mainctrl = (maintabbarcontroller *) self. tabbarcontroller; // display the toolbar [mainctrl showtabbar: Yes];}

Squleviewcontroller. m

-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) attributes {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {self. title = @ "square";} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor greencolor];}

Searchviewcontroller. m

Commentviewcontroller. m

Messageviewcontroller. m

The three files are consistent with squleviewcontroller. m, and only the title and backgroundcolor are set.



Two ways to customize the label Toolbar

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.