码迷,mamicode.com
首页 > 其他好文 > 详细

抽屉栏

时间:2015-04-30 21:59:33      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:抽屉栏

首先倒入如下文件

#import "AppDelegate.h"
#import "MainViewController.h"
#import "LeftViewController.h"
#import "MMDrawerController.h"
#import "MMExampleDrawerVisualStateManager.h"
#import "RightTableViewController.h"

在appdelegate里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //根控制器
    MainViewController *main = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];

    //导航控制器
    UINavigationController *NC = [[UINavigationController alloc]initWithRootViewController:main];


    LeftViewController *leftVC = [[LeftViewController alloc]initWithNibName:@"LeftViewController" bundle:nil];

    RightTableViewController *rightVC = [[RightTableViewController alloc]initWithNibName:@"RightTableViewController" bundle:nil];

   // MMDrawerController *drawerController = [[MMDrawerController alloc] initWithCenterViewController:NC leftDrawerViewController:leftVC];


    //用MMDrawerController 把导航控制器 包装成一个子控制器
    MMDrawerController *drawerController = [[MMDrawerController alloc]initWithCenterViewController:NC leftDrawerViewController:leftVC rightDrawerViewController:rightVC];


    //设置抽屉栏的抽出的宽度
    [drawerController setMaximumLeftDrawerWidth:243];
    [drawerController setMaximumRightDrawerWidth:243];

    //关闭和打开抽屉栏方式
    [drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    [drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];

    //设置抽屉控制器的alpha从0到1的动画效果 没有这写代码 就没有那些效果
    [drawerController
     setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible)
     {
         MMDrawerControllerDrawerVisualStateBlock block;
         //单例
         block = [[MMExampleDrawerVisualStateManager sharedManager]
                  drawerVisualStateBlockForDrawerSide:drawerSide];
         if(block)
         {
             block(drawerController, drawerSide, percentVisible);
         }
     }];
    self.window.rootViewController = drawerController;

    return YES;
}
在主界面上的左右按钮,用来促发左右抽屉
-(void)initButtonView{

    // Do any additional setup after loading the view from its nib.
    /**
     *  初始化拉开左抽屉栏的按钮
     */
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 60, 30);
    [leftBtn setTitle:@"左抽屉" forState:UIControlStateNormal];
    [leftBtn setTintColor:[UIColor redColor]];
    [leftBtn setBackgroundColor:[UIColor redColor]];
    //[leftBtn setImage:[UIImage imageNamed:@"bar_user@2x"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(btn_leftBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];



    /**
     *  初始化拉开右抽屉栏的按钮
     */
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(self.navigationController.navigationBar.frame.size.width - 30, 0, 60, 30);
    [rightBtn setTitle:@"右抽屉" forState:UIControlStateNormal];
    [rightBtn setTintColor:[UIColor redColor]];

    [rightBtn setBackgroundColor:[UIColor redColor]];
    //[leftBtn setImage:[UIImage imageNamed:@"bar_user@2x"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(btn_rightBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];


}

- (void)btn_leftBtn
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}


-(void)btn_rightBtn{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];

}
上图吧

技术分享

技术分享

技术分享

抽屉栏

标签:抽屉栏

原文地址:http://blog.csdn.net/tubiebutu/article/details/45398681

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!