Search code examples
iosswiftuinavigationcontrollernavigationnavigation-drawer

Swift Navigation with Drawer Controller


I have the following use case. I am using the KYDrawerController library. I made a Drawer menu and its working nicely. My MainViewController is a UINavigationController. On the Toolbar/Navigation bar I got a hamburger icon to open the drawer menu. The drawer menu has items and when the user clicks it, it should open a screen.

In Android one would use fragments to replace the content when an item is selected, in that case the toolbar keeps the hamburger icon and the drawer controller can still easily be opened.

How could I implement this on Swift, is it a good idea to use a UINavigationController or is there a better alternative. The requirements are the following.

  • When an item is selected, the toolbar title has to change and the drawer menu must still be accessible.
  • When an item is selected, the content has to be replaced (with a ViewController)
  • Each 'content' view controller can lead to subviews (detail/article view) in that case the hamburger icon should be replaced with a back button
  • Should have no memory leaks

What would be the best solution for this? And does somebody have a code example for this.


Solution

  • As per there code, they are already using UINavigationController to load selected menu option.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
        [tableView deselectRowAtIndexPath:newIndexPath animated:YES];
    
        KYDrawerController *elDrawer = (KYDrawerController*)self.navigationController.parentViewController;
        UIViewController *viewController;
        switch ([newIndexPath row]) {
            case 0:{
    
                viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"VC1"];                
    
                break;
            }
    
            case 1:{
    viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"VC2"];                break;
            }
    
            default:{
                viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"VC3"];                
                break;
            }
    
    
    
    
        }
    
        UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:viewController];
    
         elDrawer.mainViewController=navController;
        [elDrawer setDrawerState:DrawerStateClosed animated:YES];
    }
    

    Controllers which you want to load from side menu, you need to add a leftbarbutton item as Drawer button. In that button's selector, you need to add following line of code.

    - (IBAction)clickedOpen:(id)sender {
    
        KYDrawerController *elDrawer = (KYDrawerController*)self.navigationController.parentViewController;
        [elDrawer setDrawerState:DrawerStateOpened animated:YES];
    }