RefrostedViewController is used to show the side menu.
For Refrosted View controller first we have to install pod ,we can install this version i.e pod 'REFrostedViewController', '~> 2.4’.
We need one Root Controller in which we can add a navigation button and that would be the menu button. When we click on it then button will show the side menu.
We need three controllers that are given below->
1) Root Controller-> It will be Refrosted type ,and in this controller we add menu button
2) Content View Controller-> It is used to load different controllers.
3) Menu View controller->It is used to display menu.
First we have to add one file of Refrosted type that would be our RootController ,we have given a name i.e DEMORootViewController. Embed a NavigationController to it and add one button on navigation bar and give action to it in DEMORootViewController.
#import "REFrostedViewController.h"
@interface DEMORootViewController : REFrostedViewController
- (IBAction)showMenu:(id)sender;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *_menuButtonOutlet; //outklet of the same button
@end
In Implementation part write below code.
//
// DEMORootViewController.m
// RefrostedViewController
//
// Created by jyoti on 25/04/16.
// Copyright 2016 jyoti. All rights reserved.
//
#import "DEMORootViewController.h"
@interface DEMORootViewController ()
@end
@implementation DEMORootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)awakeFromNib
{
self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentController"];//content controller is that controller which is visible in the background of the menu controller and in content controller other controllers will load, so here we are giving storyboard id to the content controller
self.menuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"menuController"];// give storyboard id of menu in which tableview is present
}
- (void)panGestureRecognized:(UIPanGestureRecognizer *)sender
{ //this method is used to display the side menu on swapping the mouse its like Photos app, where we use our fingers to slide from one photo to another.
[self.frostedViewController panGestureRecognized:sender];
}
- (IBAction)showMenu:(UIButton *)sender {
if (sender.selected) {
[self hideMenuViewController];
sender.selected=NO;
}else{
[self presentMenuViewController];
sender.selected=YES;
}
}
@end
Now add a UIVIewController and that would be our MenuViewController, give storybord Id to it as shown in below image. Now add a table view and a prototype cell on MenuViewController. Add a class for table view cell and give identifier to the cell i.e "cell" . In MenuViewController write below code.
//
// MenuViewController.m
// RefrostedViewController
//
// Created by jyoti on 25/04/16.
// Copyright 2016 jyoti. All rights reserved.
//
#import "MenuViewController.h"
@interface MenuViewController ()
{
NSArray *mutarr;// mutable array to display content on table view
}
@end
@implementation MenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
mutarr = [[NSArray alloc]initWithObjects:@"Home",@"Profile",@"Chat",@"Friends Online",@"John Appleased",@"Profile",@"John Deo", nil];// data which we have to display
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// this method is used to display the number of rows in table view ,thsi method will called when you give data source and delegate methods to the table view
return mutarr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MenuTableViewCell *cell;
// this method is used to display the content in cell ,it will call everytime when the cell will load
NSString *cellIdentifier = @"cell"; // give cell identifier to the tableview cell
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [mutarr objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// this method is used to display the different controllers by clicking on different cells .
if (indexPath.row==1) {
Display1Controller *comments = [self.storyboard instantiateViewControllerWithIdentifier:@"Display1Controller"]; //@"Display1Controller" it is the storyBoard id of Display1controller
[self.frostedViewController setContentViewController:comments];//set Display1Controller's instance to the frostedViewController
[self.frostedViewController hideMenuViewController]; //used to hide side menu
}else if (indexPath.row==2)
{ Display2ViewController *comments = [self.storyboard instantiateViewControllerWithIdentifier:@"Display2ViewController"];
[self.frostedViewController setContentViewController:comments];
[self.frostedViewController hideMenuViewController];
}else if (indexPath.row==3){
Display3ViewController *comments = [self.storyboard instantiateViewControllerWithIdentifier:@"Display3ViewController"];
[self.frostedViewController setContentViewController:comments];
[self.frostedViewController hideMenuViewController];
}else{
ContentViewController *comments = [self.storyboard instantiateViewControllerWithIdentifier:@"contentController"];
[self.frostedViewController setContentViewController:comments];
[self.frostedViewController hideMenuViewController];
}
}
@end
Add one more UIViewController and make a class for it i.e ContentViewController. This is used to load different controllers. Give storyboard Id also i.e contentController.
Now add three other controllers i.e Display1Controller, Display2Controller and Display3Controller and add three classes of UIViewController types.
when u run the code You will get side menu and you can also Download the project.
0 Comment(s)