年初公司比較清閒,翻到一個不是自己接手的舊專案裡有抽屜效果的控制器,覺得有點意思,就研究起 MMDrawerController 來了。一方面方便日後忘記時查閱,另一方面也希望對大家有所幫助(過去做的專案大多是「上方導航欄 + 下方 tabbar」的結構,對抽屜式控制器比較陌生)。

步驟一:導入庫
先到 GitHub 把 MMDrawerController 下載下來導入專案,當然用 CocoaPods 也可以,看你習慣。

步驟二:在 AppDelegate 中配置
先導入各個控制器:
#import"MMDrawerController.h" #import"rightViewController.h" #import"centerViewController.h" #import"leftViewController.h" #import"MainNavViewController.h"
接著在 didFinishLaunching 中設定相關控制。其實與一般專案的差別,就是多了一個抽屜控制器:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { self.window= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds]; //左中右三個控制器 rightViewController*rightVc = [[rightViewControlleralloc]init]; leftViewController*leftVc = [[leftViewControlleralloc]init]; centerViewController*centerVc = [[centerViewControlleralloc]init]; //導航控制器 MainNavViewController*rightNavVc = [[MainNavViewControlleralloc]initWithRootViewController:rightVc]; MainNavViewController*leftNavVc = [[MainNavViewControlleralloc]initWithRootViewController:leftVc]; MainNavViewController*centerNavVc = [[MainNavViewControlleralloc]initWithRootViewController:centerVc]; //抽屜控制器 self.mmDrawerController= [[MMDrawerControlleralloc]initWithCenterViewController:centerNavVcleftDrawerViewController:leftNavVcrightDrawerViewController:rightNavVc]; // 關閉模式手勢 self.mmDrawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll; // 打開模式手勢 self.mmDrawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll; // 抽屜控制器的最長寬度 self.mmDrawerController.maximumLeftDrawerWidth = 200; [self.windowmakeKeyAndVisible]; self.window.rootViewController=self.mmDrawerController; returnYES; }
寫到這裡,抽屜效果的基本功能其實已經完成了。但要達到下圖那樣的呈現,還得再補一點程式碼。

接著在中央控制器的導航欄 leftBarButton 上自訂一個按鈕並加上點擊事件(記得導入相關類別):
#import "UIViewController+MMDrawerController.h" - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"Demo"; self.view.backgroundColor = [UIColor greenColor]; //UIBarButtonItem的自定義的分類方法 self.navigationItem.leftBarButtonItem = [UIBarButtonItem initWithTarget:self action:@selector(leftBtnClick) image:@"菜單 (1)" hightImage:@"菜單"]; } -(void)leftBtnClick{ // 將左邊的控制器打開 [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil]; }
再來是左側控制器的程式碼,只是在 view 上加了一個 tableView:
#import "leftViewController.h" #import "pushViewController.h" #import "UIViewController+MMDrawerController.h" #import "MainNavViewController.h" @interface leftViewController ()<UITableViewDelegate,UITableViewDataSource> @end @implementation leftViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor blueColor]; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; } cell.detailTextLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row]; return cell; }
點擊 cell 跳轉控制器:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ pushViewController *pushVc = [[pushViewController alloc] init]; pushVc.title = [NSString stringWithFormat:@"%zd",indexPath.row]; //取到center控制器 MainNavViewController *mainNavVc = (MainNavViewController *)self.mm_drawerController.centerViewController; [mainNavVc pushViewController:pushVc animated:YES]; //關閉了控制器之後記得將模式設置為None [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) { [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone]; }]; }
最後,別忘了在中央控制器的 viewDidAppear 中重新打開滑動手勢:
-(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll]; }
以上就是本文的全部內容,希望對你的學習有所幫助。