1.MMDrawerController是一个简单实用的侧边栏第三方类库。
2.在appdelegate页中初始化你需要的左右侧边栏,leftViewController ,mainViewController。
3.在appdelegate中导入头文件#import "MMDrawerController.h"
4.初始化抽屉控制器:
MMDrawerController * drawerController = [[MMDrawerController alloc] initWithCenterViewController: mainViewControllerleftDrawerViewController:leftViewController];
注:由于在MMDrawerController中机会没有涉及到storyboard的内容,如果想用xib来初始化该怎么办呢,方式如下:
以main.storyboard为例:
1.初始化:UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"main" bundle: nil];
2.添加到MMDrawerController中:drawerController = [[MMDrawerController alloc] initWithCenterViewController:[mainStoryboard instantiateViewControllerWithIdentifier:@"centerNav"] leftDrawerViewController:[mainStoryboard instantiateViewControllerWithIdentifier:@"menu"]];
5.设置抽屉的一些属性
//设置左抽屉抽出的宽度
drawerController.maximumLeftDrawerWidth = 200;
//设置抽屉视图VC阴影效果
self.mmDrawController.showsShadow = YES;
6.添加滑动手势,通过滑动手势拉出和收回抽屉。
[drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
7.在中心视图添加按钮
1)在中心视图添加头文件
#import "UIViewController+MMDrawerController.h"//第三方封装的头文件
#import "MMDrawerBarButtonItem.h"//第三方封装的头文件
#import "LeftDrawerTableViewController.h"、、左视图头文件
2)创建按钮的方法
-(void)setupLeftMenuButton
{
//创建按钮
MMDrawerBarButtonItem * leftDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(leftDrawerButtonPress:)];
//为navigationItem添加LeftBarButtonItem
[self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
}
3)添加按钮的动作
//抽屉按钮动作
-(void)leftDrawerButtonPress:(id)sender
{
//开关左抽屉
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}