三分钟学会侧滑菜单栏

一、先来看效果:

    

二、废话不多说,首先就是搭建这个第三方框架环境,只需在你的Podfile文件中添加一行 pod 'ios-Slide-Menu'即可。

三、创建ViewController,可只左划、可只右划、可左右划,按需创建相应的ViewController。

四、添加左划右划相应方法

 1 //
 2 //  CenterMenuController.m
 3 //  JoyfulPet
 4 //
 5 //  Created by Aida He on 2017/6/16.
 6 //  Copyright © 2017年 Aida He. All rights reserved.
 7 //
 8 
 9 #import "CenterMenuController.h"
10 
11 @interface CenterMenuController ()
12 
13 @end
14 
15 @implementation CenterMenuController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     self.title = @"首页";
20 }
21 
22 //允许左滑
23 - (BOOL)slideNavigationControllerShouldDisplayLeftMenu
24 {
25     return YES;
26 }
27 //允许右滑
28 - (BOOL)slideNavigationControllerShouldDisplayRightMenu
29 {
30     return YES;
31 }
32 
33 - (void)didReceiveMemoryWarning {
34     [super didReceiveMemoryWarning];
35     // Dispose of any resources that can be recreated.
36 }
37 
38 /*
39 #pragma mark - Navigation
40 
41 // In a storyboard-based application, you will often want to do a little preparation before navigation
42 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
43     // Get the new view controller using [segue destinationViewController].
44     // Pass the selected object to the new view controller.
45 }
46 */
47 
48 @end

五、插入代码到AppDelegate中

 1 #import "SlideNavigationController.h"
 2 #import "LeftMenuController.h"
 3 #import "CenterMenuController.h"
 4 #import "RightMenuController.h"
 5 
 6 @interface AppDelegate ()
 7 
 8 @end
 9 
10 @implementation AppDelegate
11 
12 
13 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
14     // Override point for customization after application launch.
15     
16     [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
17     
18 
19     [self.window setRootViewController:[[SlideNavigationController alloc] initWithRootViewController:[CenterMenuController new]]];
20     RightMenuController *rightMenu = [RightMenuController new];
21     LeftMenuController *leftMenu = [LeftMenuController new];
22     [SlideNavigationController sharedInstance].leftMenu = leftMenu;
23     [SlideNavigationController sharedInstance].rightMenu = rightMenu;
24     
25     [SlideNavigationController sharedInstance].portraitSlideOffset = 150;26     [SlideNavigationController sharedInstance].leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_menu"] style:UIBarButtonItemStyleDone target:[SlideNavigationController sharedInstance] action:@selector(toggleLeftMenu)];
27     [SlideNavigationController sharedInstance].rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_menu"] style:UIBarButtonItemStyleDone target:[SlideNavigationController sharedInstance] action:@selector(toggleRightMenu)];
28     
29     return YES;
30 }

解析:1,[SlideNavigationController sharedInstance].portraitSlideOffset设置的是左/右划时,显示主页面的偏移量

2,[SlideNavigationController sharedInstance].leftBarButtonItem/rightBarButtonItem便是设置左右菜单导航按钮

PS:三分钟过去了,你学会了吗?当然还有一些高阶用法,持续更新....

posted @ 2017-06-16 17:20  艾达  阅读(568)  评论(0编辑  收藏  举报