RESideMenu 是 iOS 上相當好用的左右側滑版面元件,很多專案都會拿來做「漢堡選單」。它允許在內容視圖控制器的左右兩側各掛一個選單視圖控制器:
You can add menu view controllers on both left and right sides of your content view controller.
讓 RootViewController 繼承 RESideMenu 並遵循 RESideMenuDelegate:
// RootViewController.h #import "RESideMenu.h" @interface RootViewController : RESideMenu <RESideMenuDelegate> @end
// RootViewController.m
#import "RootViewController.h"
@implementation RootViewController
- (void)awakeFromNib {
self.menuPreferredStatusBarStyle = UIStatusBarStyleLightContent;
self.contentViewShadowColor = [UIColor blackColor];
self.contentViewShadowOffset = CGSizeMake(0, 0);
self.contentViewShadowOpacity = 0.6;
self.contentViewShadowRadius = 12;
self.contentViewShadowEnabled = NO;
self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentViewController"];
self.leftMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LeftMenuViewController"];
self.delegate = self;
}
#pragma mark - RESideMenu Delegate
- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController {}
- (void)sideMenu:(RESideMenu *)sideMenu didShowMenuViewController:(UIViewController *)menuViewController {}
- (void)sideMenu:(RESideMenu *)sideMenu willHideMenuViewController:(UIViewController *)menuViewController {}
- (void)sideMenu:(RESideMenu *)sideMenu didHideMenuViewController:(UIViewController *)menuViewController {}
@end
重點是 awakeFromNib:在這裡設定 contentViewController 與 leftMenuViewController(範例是從 storyboard 載入)。完成後側滑就能用了。
RESideMenu 的預設行為是整條導覽堆疊中的每一頁都接受側滑手勢。這會跟 UINavigationController 打架:
對於第 2 點,可以關掉系統的側滑返回手勢:
self.navigationController.interactivePopGestureRecognizer.enabled = YES; self.navigationController.interactivePopGestureRecognizer.delegate = nil;
但 RESideMenu 的側滑並不會因此停止。
RESideMenu 有一個 BOOL 屬性 panGestureEnabled,可以視為側滑效果的總開關。只要在各頁面進出時切換它,問題就解決了。這裡用 NSNotificationCenter 做解耦,RootViewController 不必知道有哪些子頁面:
// RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(disableRESideMenu)
name:@"disableRESideMenu"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(enableRESideMenu)
name:@"enableRESideMenu"
object:nil];
}
- (void)enableRESideMenu { self.panGestureEnabled = YES; }
- (void)disableRESideMenu { self.panGestureEnabled = NO; }
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
在需要禁止側滑的頁面發送通知即可:
// 關閉側滑 [[NSNotificationCenter defaultCenter] postNotificationName:@"disableRESideMenu" object:self userInfo:nil]; // 開啟側滑 [[NSNotificationCenter defaultCenter] postNotificationName:@"enableRESideMenu" object:self userInfo:nil];
通知法的好處是零耦合,缺點是「開啟」的責任分散在各頁,容易漏掉某頁沒打開。若你的專案結構單純,直接在需要的頁面操作根控制器反而更明確:
// 進入子頁面時關閉
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
RESideMenu *sideMenu = (RESideMenu *)self.navigationController.viewControllers.firstObject;
sideMenu.panGestureEnabled = NO;
}
// 回到一級頁面時恢復
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 依實際的 root 取得方式處理
}
另外提醒:不論採用哪種方式,dealloc 中移除通知觀察者是必要的,否則在舊專案(iOS 9 之前不支援自動反註冊)容易出現野指標崩潰。