Search code examples
iosxcodeios6

Sidemenu with tabBarControllers in Storyboard


Hi Fellow iOS Developers, I am a newbie developing a project with 5 tab Views and on the first and second tabs I have slide out menus using Container views from example code by Michael Frederick on his GitHub page Project Link: https://github.com/mikefrederick/MFSideMenu. He is using a nib (.xib) files though I am using Storyboard to achieve the same and struck with defining the container and child views. can kindly some one advice how to modify the below code to accommodate in my storyboard.

the original code in the AppDelegate.m is

- (DemoViewController *)demoController {
return [[DemoViewController alloc] initWithNibName:@"DemoViewController" bundle:nil];
}

- (UINavigationController *)navigationController {
return [[UINavigationController alloc]
        initWithRootViewController:[self demoController]];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UITabBarController *tabBarController = [[UITabBarController alloc] init];

[tabBarController setViewControllers:[NSArray arrayWithObjects:[self navigationController],
                                      [self navigationController], nil]];

SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
SideMenuViewController *rightSideMenuController = [[SideMenuViewController alloc] init];

MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
                                                    containerWithCenterViewController:tabBarController
                                                 leftMenuViewController:leftSideMenuController
                                                rightMenuViewController:rightSideMenuController];

self.window.rootViewController = container;
[self.window makeKeyAndVisible];

return YES;
}

@end
  1. how to modify the code to accommodate the container parent view and child views ?
  2. where should i instantiate the code for the parent and child of the 2nd tab view ? in AppDelegate or the View Controller ?

If any other Details are required leave a comment please. Any Help Will be greatly appreciated. thanks in Advance.


Solution

  • I don't know if you still need this, but i had the exactly same problem today, too. What you need to do is:

    • remove the both methods over your app Delegate

    put this in your app Delegate:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YOUR_STORYBOARD" bundle:[NSBundle        mainBundle]];
    
    MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
    
    UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:@"THE_IDENTITY_OF_YOUR_SIDEMENU"];
    
    UITabBarController *centerViewController = [storyboard instantiateViewControllerWithIdentifier:@"IDENTITY_OF_YOUR_TABBARCONTROLLER"];
    
    [container setCenterViewController:centerViewController];
    [container setLeftMenuViewController:leftSideMenuViewController]; //for the right Side, its the same way...
    [container setPanMode:MFSideMenuPanModeNone]; //remove this line, if you need the pan mode
    return YES;
    

    In your Storyboard you have to put a ViewController as a subclass from "MFSideMenuContainerViewController". Mark this View as the "Initial View Controller" in the Attribute Inspector. Now use a Segue from your new Initial View Controller and let it "push" to your TabBarController. To avoid a Warning rename the Segue.

    After you have done this, you can add a UIBarButtonItem to every View, you like to add the SideMenu. In the Action Method of this UIBarButtomItem you only need to do this:

        [self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
    

    finally make sure you have a UIViewController or a UITableViewController, that is your "SideMenu" and set the right Storyboard ID.

    if you are still need help, comment this... and sorry for my english :)