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.
What would be the best solution for this? And does somebody have a code example for this.
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];
}
First of all, you should setup the KYDrawerController
to let it be the root view controller of your application, I would suggest to declare drawerController
as an instance variable in AppDelegate.swift
:
var drawerController:KYDrawerController?
Thus, in didFinishLaunchingWithOptions
method, you could implement:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
// let's say that you got your first view controller from the "Main" storyboard:
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
// asssume that controller would be "HomeViewController"
let homeVC = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navigation:UINavigationController = UINavigationController(rootViewController: homeVC)
appDelegate.drawerController = KYDrawerController(drawerDirection: .left , drawerWidth: 280)
appDelegate.drawerController.mainViewController = navigation
appDelegate.window?.rootViewController = appDelegate.drawerController
By implementing the above code, you should be able change the state of the drawer to be opened from anywhere
in the app, as follows:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.drawerController!.setDrawerState(.opened, animated: true)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.