左右侧边栏抽屉效果

首先用pod引入第三方库:

MMDrawerController

在podfile中填写

platform :ios, '7.0'

pod 'MMDrawerController'

在AppDelegate中引入头文件

#import <MMDrawerController.h>
#import "LeftViewController.h"
#import "ViewController.h"
#import "RightViewController.h"

直接粘贴代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
//    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    
    
    ViewController *vc = [[ViewController alloc] init];
    LeftViewController *leftVc = [[LeftViewController alloc] init];
    RightViewController *right = [[RightViewController alloc] init];
    UINavigationController *center     = [[UINavigationController alloc] initWithRootViewController:vc];
    center.delegate = (id<UINavigationControllerDelegate>)self;
    
    MMDrawerController * drawerController = [[MMDrawerController alloc]
                                             initWithCenterViewController:center
                                             leftDrawerViewController:leftVc
                                             rightDrawerViewController:right];
    [drawerController setShowsShadow:YES];
    drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
    [drawerController setRestorationIdentifier:@"MMDrawer"];
    [drawerController setMaximumLeftDrawerWidth:270.0];
    
    self.window.rootViewController = drawerController;
    
    return YES;
}

-(void)navigateToView:(UIViewController*)controler ifLeft:(BOOL)ifLeft animate:(BOOL)animate
{
    MMDrawerController*menuVctler = (MMDrawerController *)self.window.rootViewController;
    UINavigationController* nav = (UINavigationController*)menuVctler.centerViewController;
    if (nav) {
        nav.delegate = (id<UINavigationControllerDelegate>)self;
        self.ifNavFromLeftMenu = ifLeft;
        if (ifLeft) {
            [nav pushViewController:controler animated:NO];
            [menuVctler closeDrawerAnimated:YES completion:^(BOOL finished) {
                
            }];
        }else{
            [nav pushViewController:controler animated:animate];
        }
    }
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if (!self.ifNavFromLeftMenu) {
        return;
    }
    
    UIViewController* rootVc = [navigationController.viewControllers objectAtIndex:0];
    if([viewController isEqual:rootVc]){
        MMDrawerController *menuVctler = (MMDrawerController *)self.window.rootViewController;
        __weak UINavigationController*nav = navigationController;
        __weak typeof(self)wself = self;
        [menuVctler openDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
            nav.delegate = nil;
            self.ifNavFromLeftMenu = NO;
            
            double delayInSeconds = 0.2;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                wself.isNavFinished  = YES;
            });
        }];
    }
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if (!self.ifNavFromLeftMenu) {
        return;
    }
}
@interface AppDelegate ()
@property(nonatomic,assign) BOOL ifNavFromLeftMenu;

 @property(nonatomic,assign) BOOL isNavFinished;

@end

在controller中粘贴代码,首先引入头文件:

#import <MMDrawerController.h>
#import <MMDrawerBarButtonItem.h>
#import "UIViewController+MMDrawerController.h"
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupLeftMenuButton];
    [self swipe];
    [self setupRightMenuButton];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"跳转" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}


- (void)swipe{
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftDrawerButtonPress:)];
    
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.view addGestureRecognizer:swipe];
}

-(void)setupLeftMenuButton{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button setTitle:@"sdj" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = rightItem;
    
}

-(void)setupRightMenuButton{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button setTitle:@"sdj" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

-(void)leftDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
-(void)rightDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
- (IBAction)btnClick:(id)sender {
    
    NSLog(@"sfdgh");
    MyViewController *my = [[MyViewController alloc] init];
    [self.navigationController pushViewController:my animated:YES];
    
}
原文地址:https://www.cnblogs.com/h-tao/p/5099512.html