ios - Which is the best approach for creating custom tab bar controller? -


i working on application more tabbarcontroller application. can not use tabbarcontroller because need custom tab handler @ bottom need custom space between items. creating custom tabbarcontroller.

i know best approach. approach in way (using storyboard , ios6) :- took uitoolbar on viewcontroller act bottom tab bar(customtabbarviewcontroller). took containerviews each tab. when user selects item on toolbar, show containerview.

please tell me if going wrong or guide best way. thanks.

you're doing very wrong. not create custom view hierarchy when can use default one.

what want create subclass of uitabbarcontroller , create .xib file contain custom tab bar - image , arbitrary amount of uibuttons (i suppose 5).

enter image description here

set tags of them, 1-5 tags, possibly custom uiview subclass, redundant in scenario, it'll fetching controls tags.

create subclass of uitabbarcontroller. you'll need have references buttons , property see button pressed last, update ui appropriately. assign different images or titles different control states, i'm using default , selected in case.

mybasetabbarcontroller.h

@interface mybasetabbarcontroller : uitabbarcontroller @property (strong, nonatomic) uibutton *btn1; @property (strong, nonatomic) uibutton *btn2; @property (strong, nonatomic) uibutton *btn3; @property (strong, nonatomic) uibutton *btn4; @property (strong, nonatomic) uibutton *btn5; @property (weak, nonatomic) uibutton *lastsender;  @property (strong, nonatomic) uiview *tabbarview;  @end 

mybasetabbarcontroller.m

first of all, create view controllers (which uinavigationcontroller subclasses in case) , assign them uitabbarcontroller's subclass viewcontrollers property.

- (id)init {   self = [super init];   if (self) {     [self setup];   }   return self; }  - (void)setup {   nsmutablearray *viewcontrollers = [nsmutablearray array];    myviewcontroller1 *viewcontroller1 = [[mystoryboardmanager storyboard1] instantiateinitialviewcontroller];   viewcontroller1.title = @"1";   [viewcontrollers addobject:viewcontroller1];    myviewcontroller2 *viewcontroller2 = [[mystoryboardmanager storyboard2] instantiateinitialviewcontroller];   viewcontroller2.title = @"2";   [viewcontrollers addobject:viewcontroller2];    uiviewcontroller *blankcontroller = [uiviewcontroller new]; // center button, performs action instead of leading controller   [viewcontrollers addobject:blankcontroller];    myviewcontroller3 *viewcontroller3 = [[mystoryboardmanager storyboard3] instantiateinitialviewcontroller];   viewcontroller3.title = @"3";   [viewcontrollers addobject:viewcontroller3];    myviewcontroller3 *viewcontroller4 = [[mystoryboardmanager storyboard4] instantiateinitialviewcontroller];   viewcontroller4.title = @"4";   [viewcontrollers addobject:viewcontroller4];    self.viewcontrollers = viewcontrollers; } 

next grab buttons you've created , assign actions them in -viewdidload method:

- (void)viewdidload {   [super viewdidload];    _tabbarview = [[[nsbundle mainbundle] loadnibnamed:@"mytabbar" owner:nil options:nil] lastobject]; // "mytabbar" name of .xib file   _tabbarview.frame = cgrectmake(0.0,                                  self.view.frame.size.height - _tabbarview.frame.size.height,                                  _tabbarview.frame.size.width,                                  _tabbarview.frame.size.height); // make overlay actual tab bar   [self.view addsubview:_tabbarview];    _btn1 = (uibutton *)[_tabbarview viewwithtag:1];   [_btn1 addtarget:self action:@selector(processbtn:) forcontrolevents:uicontroleventtouchupinside];    _btn2 = (uibutton *)[_tabbarview viewwithtag:2];   [_btn2 addtarget:self action:@selector(processbtn:) forcontrolevents:uicontroleventtouchupinside];    _btn3 = (uibutton *)[_tabbarview viewwithtag:3];   [_btn3 addtarget:self action:@selector(processbtn:) forcontrolevents:uicontroleventtouchupinside];    _btn4 = (uibutton *)[_tabbarview viewwithtag:4];   [_btn4 addtarget:self action:@selector(processbtn:) forcontrolevents:uicontroleventtouchupinside];    _btn5 = (uibutton *)[_tabbarview viewwithtag:5];   [_btn5 addtarget:self action:@selector(processbtn:) forcontrolevents:uicontroleventtouchupinside];    _lastsender = _btn1;   [self setselectedviewcontroller:self.viewcontrollers[0]]; // make first controller selected } 

add processing method:

- (void)processbtn:(uibutton *)sender {   _lastsender = sender;   [self setselectedviewcontroller:[self.viewcontrollers objectatindex:sender.tag - 1]]; } 

and override -setselectedviewcontroller: method:

- (void)setselectedviewcontroller:(uiviewcontroller *)selectedviewcontroller {   if (_lastsender != _btn3) { // check if it's not action button     (uibutton *btn in [_tabbarview subviews]) {       if ([btn iskindofclass:[uibutton class]]) {         if (btn == _lastsender) {           btn.selected = yes;         }         else {           btn.selected = no;         }       }     }   }   if ([self.viewcontrollers indexofobject:selectedviewcontroller] == 2) {     myactioncontroller *viewcontroller = [[mystoryboardmanager actionstoryboard] instantiateinitialviewcontroller];     [self presentviewcontroller:viewcontroller animated:yes completion:nil];   }   else {     if (self.selectedviewcontroller == selectedviewcontroller) {       [(uinavigationcontroller *)self.selectedviewcontroller poptorootviewcontrolleranimated:animate]; // pop root if tapped same controller twice     }     [super setselectedviewcontroller:selectedviewcontroller];   } } 

i'm assuming you're programming arc enabled , have class manages storyboards, that's pretty straightforward anyway.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -