SWRevealViewController close rear view when tapping front view

前端 未结 9 1590
夕颜
夕颜 2020-12-09 04:18

I am using SWRevealViewController in order to implement a side nav menu in my app. I would like to make it so that the front view cannot be interacted with when

相关标签:
9条回答
  • 2020-12-09 05:13

    On your Menu view controller, add this:

    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        [self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
    }
    
    -(void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
    
        [self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
    }
    

    Then on your Menu Item View Controller, add this on viewDidLoad:

    SWRevealViewController *revealController = [self revealViewController];
    [revealController tapGestureRecognizer];
    

    Also check out my answer here. It addresses the problem on front view interaction (plus a slide gesture).

    0 讨论(0)
  • 2020-12-09 05:15

    Put below code on menu view controller its works for me

    @interface SidebarTableViewController() {
        UIView* coverView;
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [coverView removeFromSuperview];
        //[self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
        // get your window screen size
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        // get your window screen size
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        //create a new view with the same size
        coverView = [[UIView alloc] initWithFrame:screenRect];
        // change the background color to black and the opacity to 0.6
        coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        // add this new view to your main view
        [self.revealViewController.frontViewController.view addSubview:coverView];
        // [self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
    }
    
    0 讨论(0)
  • 2020-12-09 05:16
    1. Subclass of SWRevealViewController.
    2. Implement revealController:willMoveToPosition: of SWRevealViewControllerDelegate.
    3. Set full screen view on front view controller to override all touches.
    4. Add tap gesture recognizer to hide menu.

    Sample with nice animation:

    - (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position;
    {
        static NSInteger tagLockView = 4207868622;
        if (revealController.frontViewPosition == FrontViewPositionRight) {
            UIView *lock = [self.frontViewController.view viewWithTag:tagLockView];
            [UIView animateWithDuration:0.25 animations:^{
                lock.alpha = 0;
            } completion:^(BOOL finished) {
                [lock removeFromSuperview];
            }];
        } else if (revealController.frontViewPosition == FrontViewPositionLeft) {
            UIView *lock = [[UIView alloc] initWithFrame:self.frontViewController.view.bounds];
            lock.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            lock.tag = tagLockView;
            lock.backgroundColor = [UIColor blackColor];
            lock.alpha = 0;
            [lock addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(revealToggle:)]];
            [self.frontViewController.view addSubview:lock];
            [UIView animateWithDuration:0.75 animations:^{
                lock.alpha = 0.333;
            }];
        }
    }
    

    This solution for old version 1.x SWRevealViewController.

    0 讨论(0)
提交回复
热议问题