這裡我們使用AMSlideMenu來實現左右側滑菜單的效果。控件支持單獨左側滑、單獨右側滑和左右側滑。同時支持Storyboard和xib兩種開發模式。這裡介紹第二種,在xib中的開發。
開發步驟如下: 1. 在Podfile中添加:pod "AMSlideMenu", "~> 1.5.3",通過pod install導入項目。 2. 在Pods項目中(注意:不是你自己的項目),在Pods-*.pch文件中添加如下一行代碼: // 必須,否則xib方式會報錯 #define AMSlideMenuWithoutStoryboards 3. 實現一個繼承了AMSlideMenuMainViewController類的ViewController。主要代碼如下: 復制代碼 - (void)viewDidLoad { /******************************* * 初始化菜單 *******************************/ self.leftMenu = [[LeftMenuTVC alloc] initWithNibName:@"LeftMenuTVC" bundle:nil]; self.rightMenu = [[RightMenuTVC alloc] initWithNibName:@"RightMenuTVC" bundle:nil]; /******************************* * 結束初始化 *******************************/ [super viewDidLoad]; // Do any additional setup after loading the view. } // 設置左側菜單按鈕樣式(右側按鈕類似操作) - (void)configureLeftMenuButton:(UIButton *)button { CGRect frame = button.frame; frame.origin = (CGPoint){0,0}; frame.size = (CGSize){40,40}; button.frame = frame; forState:UIControlStateNormal]; } 復制代碼 4. 實現一個繼承了AMSlideMenuLeftTableViewController的UITableViewController的類作為左側菜單(右側菜單類似) 主要代碼如下: 復制代碼 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // 初始化菜單項 self.tableData = [@[@"VC 1",@"VC 2",@"VC 3"] mutableCopy]; } // 點擊菜單項跳轉到不同的VC - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UINavigationController *nvc; UIViewController *rootVC; switch (indexPath.row) { case 0: { rootVC = [[FirstVC alloc] initWithNibName:@"FirstVC" bundle:nil]; } break; case 1: { rootVC = [[SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil]; } break; case 2: { rootVC = [[ThirdVC alloc] initWithNibName:@"ThirdVC" bundle:nil]; } break; default: break; } nvc = [[UINavigationController alloc] initWithRootViewController:rootVC]; [self openContentNavigationController:nvc]; } 復制代碼 5. 最後記得在AppDelegate中要做這步操作(當然,其它地方也可以): 復制代碼 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { MainVC *mainVC = [[MainVC alloc] init]; UINavigationController *startNVC = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = startNVC; [self.window makeKeyAndVisible]; return YES; }