Last updated on July 31st, 2016 at 11:13 am
In this tutorial, I will show you how create a slide-out (side) menu in iOS application, similar to the one you will see in Facebook or Gmail application . In this post i am using custom library for implementing side menu which is MF SIDEMENU .
Step 1 :
Download library from Github and extract the zipped file. Copy all the classes to your new project .
Classes of MFSideMenu will include (Both .h / .m files) :-
- MFSideMenu
- MFSideMenuContainer
- MFSideMenuShadow
- UIViewController+MFSideMenuAdditions
Add these classes to your new project.
OR
You can add pod 'MFSideMenu'
to your Podfile.
For adding pod
- Open terminal .
- Write cd space DRAG FOLDER TO TERMINAL.
- Write the following command in the terminal .
- pod init // This will initialize the pod.
- open -a Xcode Podfile // This will open the PodTextFile.
- Then add the following code in the text file
pod 'MFSideMenu' .
- Write pod install in the terminal
- Wait for few minutes ,After that Pod installation is been completed.
After pod installation , a workspace is created for your project . Start using workspace instead of the project file .
You can either use pod or can copy files to the Xcode itself.
Step 2 :
Create a new objective -c class which will contain the list of items of side menu.
- Press cmd + n to create a new objective c class and name it SideMenuViewController
- Make the class of type UITableViewController
- Add the following code in .h file of the SideMenuViewController.
- Create a global array which will contain the list of items go the sideMenu.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface SideMenuViewController : UITableViewController | |
{ | |
NSArray *MenuArray; | |
} |
Step 3:
In the .m file of SideMenuViewController
- Import the MFSideMenu.h
- Add the items in the global array and create a tableView which will hold the list of items of the sideMenu.
- Add tableView`s data source to display data in your menu .
- Code of .m file of SideMenuViewController will look like this :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation SideMenuViewController | |
{ | |
UITableView *tableView; | |
} | |
#pragma mark – Did Load | |
#pragma mark – | |
-(void) viewDidLoad | |
{ | |
[super viewDidLoad]; | |
MenuArray =[NSArray arrayWithObjects:@"Profile",@"Friends",@"Status",@"Settings",@"Logout",nil]; | |
self.tableView.backgroundColor = [UIColor whiteColor]; | |
self.tableView.alwaysBounceVertical = NO; | |
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; | |
} | |
#pragma mark – UITableViewDataSource | |
#pragma mark – | |
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | |
return 1; | |
} | |
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return MenuArray.count; | |
} | |
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
cell.backgroundColor=[UIColor clearColor]; | |
cell.textLabel.textColor=[UIColor blackColor]; | |
UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(30, 0, 270, 1)]; | |
lineView.backgroundColor=[UIColor whiteColor]; | |
[cell.contentView addSubview:lineView]; | |
} | |
cell.textLabel.text = [MenuArray objectAtIndex:indexPath.row]; | |
return cell; | |
} | |
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
return 40; | |
} | |
@end |
Step 4:
Go to AppDelegate
- Import ViewController.h and SideMenuViewController.h
- Add the following code to add SideMenuViewController to mainController.(In this controller i am adding SideMenuViewController to)
- Import MFSideMenu.h in AppDelegate.h file.
- Code for AppDelegate.m is this .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "AppDelegate.h" | |
#import "ViewController.h" | |
#import "SideMenuViewController.h" | |
@interface AppDelegate () | |
{ | |
UINavigationController *navigation; | |
} | |
@end | |
@implementation AppDelegate | |
– (void)applicationDidFinishLaunching:(UIApplication *)application | |
{ | |
ViewController *viewCtrl=[[ViewController alloc]init]; | |
navigation=[[UINavigationController alloc]initWithRootViewController:viewCtrl]; | |
navigation.navigationBar.barTintColor = [UIColor colorWithRed:72.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1]; | |
SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init]; | |
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController | |
containerWithCenterViewController:navigation | |
leftMenuViewController:leftMenuViewController | |
rightMenuViewController:nil]; | |
self.window.rootViewController=container; | |
} |
Step 5 :
Go to ViewController.m, as i dont want to see navigation bar at this time , I add the following code. You can modify this code accordingly your requirements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
– (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
self.navigationController.navigationBarHidden = YES; | |
self.view.backgroundColor = [UIColor whiteColor]; | |
self.view.frame = [[UIScreen mainScreen] bounds]; | |
} |
Step 6 :
Swipe Right to see SideMenu in your project.
You can also show SideMenu on button`s click by adding this code in UIButton Action :
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
Thank You 🙂