Reputation: 22725
I have found a tab bar library that I would like to use, but need to use storyboards for my application.
Upon seeing that this library works with xibs, I took it upon myself to adapt it to work with my storyboard app. However, I could not make it work.
The library can be found here : http://www.cocoacontrols.com/controls/tbtabbar
So how can I adapt the library to make it work with storyboards, and if that cannot be done, how can I use a uinavigationcontroller inside of the mainwindow, as I can't get that to work as well.
Thanks
EDIT: This is the code that swaps view controllers:
-(void)switchViewController:(UIViewController *)viewController {
UIView *currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG];
[currentView removeFromSuperview];
viewController.view.frame = CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height-(tabBar.frame.size.height));
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
[self.view insertSubview:viewController.view belowSubview:tabBar];
}
Upvotes: 0
Views: 116
Reputation: 931
I completely reworked my answer and created a sample project to help make sure I didn't miss anything the second time around.
You should be able make all of your view controllers (with identifiers) in the storyboard subclasses of TBViewController. This can be done by clicking on your UIViewController, going to the identity inspector (cmd alt 3) and then typing in "TBViewController". It should autocomplete if everything is working correctly. The next step is to create the tabs for your tab controller. Each tab needs 3 values before it can be added to the TBTabBar controller.
Below is a custom method I have created to help instantiate these TBTabButton objects which accept all three requirements as paramaters:
-(TBTabButton*)TBTabButtonWithStoryBoardID:(NSString*)identifier andButtonImageNamed:(NSString*)normalImageTitle andHighlightedImageNamed:(NSString*)highlightImageTitle{
//Create the tab bar and assign normal image
TBTabButton *tabBarButtonItem = [[TBTabButton alloc] initWithIcon:[UIImage imageNamed:normalImageTitle]];
//Assign Highlighted image
tabBarButtonItem.highlightedIcon = [UIImage imageNamed:highlightImageTitle];
//Fetch the subclassed view controller from storyboard and assign as viewController
tabBarButtonItem.viewController = [self TBViewControllerFromStoryboardWithIdentifier:identifier];
return tabBarButtonItem;
}
The View controllers can be retrieved from storyboard by using the method in the next code block.
-(TBViewController*)TBViewControllerFromStoryboardWithIdentifier:(NSString*)identifier{
//instantiateViewControllerWithIdentifier returns a UIViewController Object
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
//check if UIViewController is a subclassed TBViewController
if([viewController isKindOfClass:[TBViewController class]]){
//if it is then send it back as the correct type
return (TBViewController*)viewController;
}
return nil;
}
Now you have a method which will create for you 'ready-to-add' TBTabButton objects. Use this method to create each desired button for your tab bar. Once you have these instantiated, you must add them to an NSArray so that it may be passed as a paramater for the creation of the TBTabBar object. All that is left is to add the UITabBar object to the view and set it to display based on defaults.
-(void)setTBTabBar{
//Instantiate your tab TBTabButtons
TBTabButton *tab1 = [self TBTabButtonWithStoryBoardID:@"tab1"
andButtonImageNamed:@"favoritesIcon.png"
andHighlightedImageNamed:@"favoritesIconHighlighted.png"];
TBTabButton *tab2 = [self TBTabButtonWithStoryBoardID:@"tab2"
andButtonImageNamed:@"mentionsIcon.png"
andHighlightedImageNamed:@"mentionsIconHighlighted.png"];
TBTabButton *tab3 = [self TBTabButtonWithStoryBoardID:@"tab3"
andButtonImageNamed:@"messagesIcon.png"
andHighlightedImageNamed:@"messagesIconHighlighted.png"];
//Add them to an array
NSArray *arrayOfTBViewControllers = [NSArray arrayWithObjects: tab1, tab2, tab3, nil];
//Instantiate your TBTabBar object with your array of TabButtons. Set this view as delegate
tabBar = [[TBTabBar alloc] initWithItems:arrayOfTBViewControllers];
tabBar.delegate = self;
//Add it to the view and set it to show defaults
[self.view addSubview:tabBar];
[tabBar showDefaults];
}
This method must be called in your -(void)viewDidLoad method;
Only other things that need to be done are:
Hopefully this addresses any issues were you having. This seemed to be the one thing that might be a little confusing. As long as you are following the other steps in the README correctly, I believe this should work just dandy for you.
If that doesn't work, let me know and I can try to post a link to the working example I made for the code above.
Upvotes: 1