Storyboard based UITabBarController.
Want to create a custom UITabBarController from the interface builder? Here is your solution.
Drag these files into your project:
"SBTabbarController.m"
"SBTabbarController.h"
Then import the header into your project where needed:
#import "SBTabbarController.h"
For SBTabbarController to function, create a UIViewController in the interface builder; in this you will create your navigation system (I'm going to use a sidebar as an example):
We need to create an active view in the controller.. when we change tabs or push controllers onto the stack, this is where they will be displayed - so feel free to customise the frame.
All we have to do, is set the Restoration ID of this view to "SBTabbarController":
Now we have our controller setup, we just need to initialise a SBTabbarController instance and set the controllers.
We will simply create a UIViewController class for our custom controller:
Now in our custom class.. we can setup our SBTabbarController:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupTabbarController];
}
#pragma mark - Custom Tabbar controller setup:
- (void)setupTabbarController {
SBTabbarController *tabBar = [[SBTabbarController alloc] initWithController:self];
UIViewController *first = [UIViewController new];
UIViewController *second = [UIViewController new];
UIViewController *third = [UIViewController new];
UIViewController *fourth = [UIViewController new];
NSArray *controllers = @[first, second, third, fourth];
[tabBar setTabbarControllers:controllers];
}
Let's say that we've setup custom actions for our example buttons (tab1, tab2, etc) - when we click these 'tabs', we want to change to a tab in the controllers that we set. Easily done.
- (IBAction *)changeToTab1 {
[[SBTabbarController sharedInstance] changeTab:0];
}
- (IBAction *)changeToTab2 {
[[SBTabbarController sharedInstance] changeTab:1];
}
SBTabbarController also supports a navigation stack - using the same "active" view.
Similar methods to UINavigationController have been created:
- (IBAction *)toToNextScreen {
UIViewController *nextController = [UIViewController new];
[[SBTabbarController sharedInstance] pushViewController:nextController];
}
- (IBAction *)goBack {
[[SBTabbarController sharedInstance] popViewController];
}
- (IBAction *)replaceScreen {
UIViewController *replacement = [UIViewController new];
[[SBTabbarController sharedInstance] replaceViewController:replacement animated:YES];
}
- (IBAction *)unwindNavigationStack {
[[SBTabbarController sharedInstance] 0];
}
## Credits:
iOSDec