First, the idea of customization
Tabbarcontroller in iOS is really powerful, and most of the mainstream iOS apps are going to use them. However, it is often not enough to meet all the requirements, so you need to customize the Tabbar, customization needs to have a good understanding of the way the system Tabbar work, the customization needs courage.
Custom Tabbar Principle: Try to use the system to bring the Tabbar, only change the need to change the place.
Ii. The overall process of customizing Tabbar 1. the TabBar strip was first canceled . 2. make a view, put a few buttons on it, Set the button click event . and set Selectindex. 3. Associate each sub-viewcontroller, covering related events.
Third, the details are important
1. Associate the button you have created with Viewcontroller: ? Use the tabbar selectedindex Property . set this property on the line. . 2. Remove the system highlighting : ? You can customize a button . rewrite the inside sethighhighted Method , just don't do anything. . ( if calling Super is equivalent to not writing ) 3. for a few buttons select only one method : ? set a property , record the last selected button .? When you click the current button , Set the previous button to unchecked , and sets the current button to the selected , finally assign the current button to the previous button .
Iv. Preliminary Customizationdirectly on the code, see comments.
XNTabBarController.h
#import <UIKit/UIKit.h> @interface Xntabbarcontroller:uitabbarcontroller@end
Xntabbarcontroller.m
xntabbarcontroller.m//////Created by Neng on 14-6-19.//Copyright (c) 2014 Neng. All rights reserved.//#import "XNTabBarController.h" #import "Common.h" #import "XNTabBarButton.h" @interface Xntabbarcontroller ()/** * Set the previously selected button */@property (nonatomic, weak) UIButton *selectedbtn; @end @implementation xntabbarcontroller-(void) viewdidload {[Super viewdidload];//below two methods are often used in development//NSLog (@ "%s", __func__);//NSLog (@ "% @ ", self.view.subviews); Can print out all sub-views, and its framelogfun; Logsubviews (Self.view);//delete existing tabbarcgrect rect = Self.tabBar.frame; [Self.tabbar Removefromsuperview]; Remove Tabbarcontroller from the lower bar//test add own view UIView *myview = [[UIView alloc] init];myview.frame = rect; Myview.backgroundcolor = [Uicolor Redcolor]; [Self.view addsubview:myview];for (int i = 0; i < 5; i++) {//uibutton *btn = [[UIButton alloc] init]; Xntabbarbutton *btn = [[Xntabbarbutton alloc] init]; NSString *imagename = [NSString stringwithformat:@ "tabbar%d", i + 1]; NSString *imagenamesel = [NSStringstringwithformat:@ "Tabbar%dsel", i + 1]; [btn Setimage:[uiimage Imagenamed:imagename] forstate:uicontrolstatenormal]; [btn Setimage:[uiimage Imagenamed:imagenamesel] forstate:uicontrolstateselected]; CGFloat x = i * myview.frame.size.width/5;btn.frame = CGRectMake (x, 0, MYVIEW.FRAME.SIZE.WIDTH/5, myView.frame.size.he ight); [MyView ADDSUBVIEW:BTN]; Btn.tag = i;//Sets the flag of the button, it is convenient to index the current button, and jump to the corresponding View//with parameters of the listening method remember to add "colon" [btn addtarget:self Action: @selector (CLICKBTN:) forcontrolevents:uicontroleventtouchupinside];//setting just entered, the first button is selected if (0 = = i) {btn.selected = YES;SELF.SELECTEDBTN = Btn Set this button to the selected button}}}/** * Custom Tabbar button click event */-(void) CLICKBTN: (UIButton *) button {//1. First set the previously selected button to unchecked self.selectedBtn.selected = NO;//2. Then set the current button to check button.selected = Yes;//3. Finally, the current button is assigned to the previously selected button, self.selectedbtn = button; 4. Jump to the corresponding view controller. (The controller is set with the Selectindex parameter selected) Self.selectedindex = Button.tag;} @end
XNTabBarButton.h
#import <UIKit/UIKit.h> @interface Xntabbarbutton:uibutton@end
Xntabbarbutton.m
#import "XNTabBarButton.h" @implementation xntabbarbutton/** to cancel the System button's highlight state */-(void) sethighlighted: (BOOL) highlighted{// [Super sethighlighted:highlighted];} @end
Five, Code refactoring
The purpose of refactoring is to put the code where he should be . Improved read-write and extensibility.
The refactoring of controls is guaranteed to be reusable . when you do the encapsulation for other applications , you can take it straight to the point of the past. .
Tips: 1. about init and initwithframe: ? in object initialization calls Init when , is called initWithFrame Method .? Init with the initWithFrame will be called .? We recommend that custom controls not be heavy write init method initialize override initwithframe method . ? Benefits : other people call either the call Init, or call the initWithFrame will be called initWithFrame Method .
2 . About the Layout code of the control : ? suggestions are written in layoutsubviews method in .? don't forget to write Super Method ? will set X,y,frame I 'll write it in here . . 3 . Add the custom Tabbar as a sub-view of the system Tabbar , This way the TabBar Toggle Auto-Hide / swipe function does not have to be done by itself . (Hide Bottom Bar on push)
The refactoring code is as follows :Build the custom Tabbar separately and move the code over. Set the proxy method, the toolbar button is selected , and the record is where to jump from .
XNTabBar.h
#import <UIKit/UIKit.h> @class xntabbar; @protocol xntabbardelegate <nsobject>/** * toolbar button is selected, Record where to jump from. (Convenient to do the corresponding effects later) */-(void) TabBar: (Xntabbar *) TabBar Selectedfrom: (Nsinteger) from to: (Nsinteger) to; @end @interface Xntabbar:uiview@property (nonatomic,weak) id<xntabbardelegate> delegate;/** * use a specific image to create a button, the benefit of this is extensibility. Get another project inside can also change the picture directly with * * @param image Normal picture * @param selectedimage selected picture */-(void) Addbuttonwithimage: ( UIImage *) Image selectedimage: (UIImage *) selectedimage; @end
XNTABBAR.M
xntabbar.m////Created by Neng on 14-6-19.//Copyright (c) 2014 Neng. All rights reserved.//#import "XNTabBar.h" #import "XNTabBarButton.h" @interface xntabbar ()/** * Set the previously selected button */@property (n Onatomic, weak) UIButton *selectedbtn; @end @implementation xntabbar/** * Write control initialization In this method, call *///-(ID) when calling the Init method initWithFrame: (CGRect) frame {//if (self = [Super Initwithframe:frame]) {////add button//for (int i = 0; i < 5; i++) {//Cancel out specific The number////uibutton *btn = [[UIButton alloc] Init];//xntabbarbutton *btn = [[Xntabbarbutton alloc] init];////nsstring *imagen AME = [NSString stringwithformat:@ "tabbar%d", i + 1];//nsstring *imagenamesel = [NSString stringwithformat:@ "TabBar%dSel ", i + 1];////[btn setimage:[uiimage imagenamed:imagename] forstate:uicontrolstatenormal];//[btn setImage:[UIImage Imagenamed:imagenamesel] forstate:uicontrolstateselected];////[self addsubview:btn];////btn.tag = i; Set the button's mark, convenient to index the current button, and jump to the corresponding view//////with the parameters of the listening method remember to add "colon"//[btn addtarget:self action: @selector (clickbtn:) Forcontrolevents:uicontroleventtouchupinside];////if (0 = = i) {//[self clickbtn:btn];//}//}//}//return self;//}-(void) Addbuttonwithimage: (UIImage *) image selectedimage: (UIImage *) selectedimage {UIButton *btn = [[UIButton alloc] init];[ BTN Setimage:image Forstate:uicontrolstatenormal]; [Btn Setimage:selectedimage forstate:uicontrolstateselected]; [Self addsubview:btn];//with parameter's listening method remember to add "colon" [btn addtarget:self Action: @selector (clickbtn:) forControlEvents: uicontroleventtouchupinside];//if it is the first button, select (add one by one) if (Self.subviews.count = = 1) {[Self clickbtn:btn];}} /** specifically used to layout the child view, don't forget to call the Super method */-(void) layoutsubviews {[Super Layoutsubviews];int count = self.subviews.count;for (int i = 0 ; I < count; i++) {//get button UIButton *btn = Self.subviews[i];btn.tag = i;//Set the button's marker to easily index the current button and jump to the corresponding view cgfloat x = i * self.bounds.size. Width/count; CGFloat y = 0; CGFloat width = self.bounds.size.width/count; CGFloat height = Self.bounds.size.height;btn.frame = CGRectMake (x, y, width, height);}} /** * Custom Tabbar button click event */- (void) CLICKBTN: (UIButton *) button {//1. Set the previously selected button to unchecked self.selectedBtn.selected = NO;//2. Then set the current button to check button.selected = YES;//3. Finally assign the current button to the previously selected button SELF.SELECTEDBTN = button;//But change the view controller thing, should give controller to do// It is best to write this by first judging if the proxy method implements the if ([Self.delegate respondstoselector: @selector (tabBar:selectedFrom:to:)]) {[self.delegate Tabbar:self SelectedFrom:self.selectedBtn.tag To:button.tag];} 4. Jump to the corresponding view controller. (The controller is set with the Selectindex parameter selected)//self.selectedindex = Button.tag;} @end
The original
XNTABBARCONTROLLER.MAfter the modification, the original code was commented.
xntabbarcontroller.m////Created by Neng on 14-6-19.//Copyright (c) 2014 Neng. All rights reserved.//#import "XNTabBarController.h" #import "XNTabBarButton.h" #import "XNTabBar.h" @interface Xntabbarcontroller () <xntabbardelegate>/** * Set the previously selected button */@property (nonatomic, weak) UIButton *selectedbtn;@ End@implementation xntabbarcontroller-(void) viewdidload {[Super viewdidload];//The following two methods are often used in development//NSLog (@ "%s", __ FUNC__);//NSLog (@ "%@", self.view.subviews); Can print out all the child views, and its frame//logfun;//logsubviews (self.view);//hell//Delete the existing tabbarcgrect rect = self.tabBar.bounds; Here to use bounds to add, otherwise it will be added to the following. Invisible Logframe (Self.tabbar);//[self.tabbar Removefromsuperview]; Remove Tabbarcontroller from the lower bar//test to add your own view xntabbar *myview = [[Xntabbar alloc] init]; The setup agent must change the previous type and cannot use uiviewmyview.delegate = self; Set proxy myview.frame = rect; [Self.tabbar Addsubview:myview]; Add to the system's own tabbar, so you can use the event method. Instead of having to write it yourself//Add a button for the controller for (int i=0; i<self.viewcontrollers.count; i++) {//Based on how many child view controllers are addedbutton NSString *imagename = [NSString stringwithformat:@ "tabbar%d", i + 1]; NSString *imagenamesel = [NSString stringwithformat:@ "Tabbar%dsel", i + 1]; UIImage *image = [UIImage imagenamed:imagename]; UIImage *imagesel = [UIImage Imagenamed:imagenamesel]; [MyView addbuttonwithimage:image Selectedimage:imagesel]; }////Add button//for (int i = 0; i < 5; i++) {////uibutton *btn = [[UIButton alloc] init];//Xntabbarbutton *b TN = [[Xntabbarbutton alloc] init];////nsstring *imagename = [NSString stringwithformat:@ "tabbar%d", I + 1];//NSString *im Agenamesel = [NSString stringwithformat:@ "Tabbar%dsel", i + 1];////[btn setimage:[uiimage ImageNamed:imageName] FORSTATE:UICONTROLSTATENORMAL];//[BTN setimage:[uiimage Imagenamed:imagenamesel] forState:UIControlStateSelected] ;////cgfloat x = i * myview.frame.size.width/5;//btn.frame = CGRectMake (x, 0, MYVIEW.FRAME.SIZE.WIDTH/5, MyView.frame. size.height);////[myview Addsubview:btn];////Btn.tag = i;//Set the marker of the button, it is convenient to index the current button, and jump to the corresponding view//////with the parameters of the listening method remember to add "colon"//[btn addtarget:self action: @selector (CL ICKBTN:) forcontrolevents:uicontroleventtouchupinside];//////setting just entered, the first button is selected//if (0 = = i) {//btn.selected = YES;// SELF.SELECTEDBTN = BTN; Set this button to the selected button//}//}}/** never forget to set proxy */-(void) TabBar: (Xntabbar *) TabBar Selectedfrom: (Nsinteger) from to: (Nsinteger) to { Self.selectedindex = to;} /** * Custom Tabbar button click event *///-(void) CLICKBTN: (UIButton *) button {////1. First set the previously selected button to unchecked//self.selectedbtn.selected = no ;////2. Then set the current button to check//button.selected = Yes;////3. Finally, assign the current button to the previously selected button//self.selectedbtn = button;//////4. Jumps to the corresponding view controller. (The controller is set with the Selectindex parameter selected)//Self.selectedindex = button.tag;//} @end
Custom-defined:
Example Source code download : http://download.csdn.net/detail/xn4545945/7572263
Reprint Please specify Source: http://blog.csdn.net/xn4545945