SWRevealViewController - Prevent Interacting With The Back View - ios

When using SWRevealViewController, I do not want to let the user interact with the view that opened the side menu. Once the side menu opens, I want to just be able to interact with that menu view and not the other one.
Any help?

well I has been working with/on SWRevealViewController for a while
so what you need is add your frontViewController as SWRevealViewControllerDelegate and then implementing this function
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
you will be notified when frontViewController go to left or to front position
this is the Swift code
in your frontViewController you need to add
class FrontViewController: UIViewController, SWRevealViewControllerDelegate
{
override func viewDidLoad() {
super.viewDidLoad()
self.revealViewController().delegate = self;
}
//YOUR CODE//
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
if(position == FrontViewPosition.Left)
{
self.view.userInteractionEnabled = true;
self.navigationController?.navigationBar.userInteractionEnabled = true;
}else
{
self.view.userInteractionEnabled = false;
self.navigationController?.navigationBar.userInteractionEnabled = false;
}
}
//EDITED
This is the Objective C code
class FrontViewController: UIViewController <SWRevealViewControllerDelegate>
in the viewDidLoad you need to add
- (void)viewDidLoad
{
[super viewDidLoad];
self.revealViewController.delegate = self;
}
//YOUR CODE//
- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
{
if(position == FrontViewPositionLeft)
{
self.view.userInteractionEnabled = NO;
self.navigationController.navigationBar.userInteractionEnabled = NO;
}else{
self.view.userInteractionEnabled = YES;
self.navigationController.navigationBar.userInteractionEnabled = YES;
}
}
I hope this help you

Related

Control swipe back action in UINavigationController

I'm creating a simple flash card app as illustrated below:
I want a swipe backwards to occur like this:
To do this, onBack(index: Int) is what I need to be called when the swipe back happens (in order to update the card shown):
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var flashCardLabel: UILabel!
// Populate initial content
let content = ["Lorem", "Ipsum", "Dolor", "Sit"]
// Index of where we are in content
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
}
// Label text based on index
func setLabelToIndex() {
flashCardLabel.text = content[index]
}
// Go back
#IBAction func back(_ sender: Any) {
if index > 0 {
index = index - 1
setLabelToIndex()
}
}
// Go forward
#IBAction func next(_ sender: Any) {
if index + 1 < content.count {
index = index + 1
setLabelToIndex()
}
}
// Desired function to be called
// when swiping back in navigation stack
func onBack(index: Int) {
self.index = index
setLabelToIndex()
}
}
If I understand your question correctly, you want to be able to swipe between questions and/or have a swipe effect when you click "Next" or "Back". If that's the case, I suggest you embed your UILabel in a UIScrollView. Check this out:
class ViewController: UIViewController,UIScrollViewDelegate {
let content = ["Lorem", "Ipsum", "Dolor", "Sit"]
let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.contentSize = CGSize(width: self.view.frame.width * content.count, height: self.scrollView.frame.size.height)
scrollView.isPagingEnabled = true
// add labels to pages
for i in 0 ..< content.count {
let label = UILabel(frame: CGRect(x: self.view.center.x * (i + 1), y: self.view.center.y, width: 100, height: 50))
label.textAlignment = .center
label.text = content[i]
scrollView.addSubview(label)
}
self.view.addSubview(scrollView)
}
// Go back
#IBAction func back(_ sender: Any) {
if index > 0 {
index = index - 1
// scroll to page
let offset = CGPoint(x: CGFloat(index) * self.view.frame.width, y: 0)
self.scrollView.setContentOffset(offset, animated: true)
}
}
// Go forward
#IBAction func next(_ sender: Any) {
if index + 1 < content.count {
index = index + 1
// scroll to page
let offset = CGPoint(x: CGFloat(index) * self.view.frame.width, y: 0)
self.scrollView.setContentOffset(offset, animated: true)
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
index = round(scrollView.contentOffset.x / scrollView.frame.size.width)
}
}
Explanation:
You basically create a UIScrollView to handle the pagination effect and add a UILabel to each page with the respective text from the content array. Every time the user scrolls to a different page, index gets updated to the index of the current page. And finally, when the user clicks "Next" or "Back", you scroll over to the next or previous page
If you want it to by Push & Pop navigationcontroller, you can do it by making static index variable.
Here is code
class ViewController: UIViewController {
#IBOutlet weak var flashCardLabel: UILabel!
// Populate initial content
let content = ["Lorem", "Ipsum", "Dolor", "Sit"]
// Index of where we are in content
static var INDEX = 0;
override func viewDidLoad() {
super.viewDidLoad()
self.next(nil);
}
// Label text based on index
func setLabelToIndex() {
flashCardLabel.text = content[ViewController.INDEX]
}
// Go back
#IBAction func back(_ sender: Any?) {
if ViewController.INDEX > 0 {
ViewController.INDEX = ViewController.INDEX - 1
setLabelToIndex()
}
}
// Go forward
#IBAction func next(_ sender: Any?) {
if ViewController.INDEX + 1 < content.count {
ViewController.INDEX = ViewController.INDEX + 1
setLabelToIndex()
}
}
// Desired function to be called
// when swiping back in navigation stack
func onBack(index: Int) {
ViewController.INDEX -= 1;
//setLabelToIndex()
}
override func didMove(toParentViewController parent: UIViewController?) {
if parent == nil {
self.onBack(index: ViewController.INDEX);
}
}
}
i can tell you that swipe that UINavigationController suppport is the the swipe when user start swipping his finger from the left of the screen to right just to pop the view from navigation you can not push it back by swipping from right edge to left in iPhone, this is default in UINavigationController
i am writing my code as i am using you need to customize it accordinly, i didn't had time in office to edit, i will tell you more
#pragma mark for pageView
- (UIViewController *) viewControllerAtIndex:(NSUInteger)index
{
if (index > (self.imageArray.count-1))
return nil;
UIViewController *viewController = nil; ////
GalleryItems *item = self.imageArray[index];
NSString *cachedGalleryItemName = [item getCachedPhotoFileNameWithPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:cachedGalleryItemName])
{
ImageViewController *imageVC = [[ImageViewController alloc] initWithNibName:#"ImageViewController" bundle:nil];
imageVC.galleryItem = item;
imageVC.cachedGalleryItemName = cachedGalleryItemName;
imageVC.index = index;
viewController = imageVC;
}
else
{
if (self.downloadViewController)
{
if (self.indexOfDownloadInProgress == index)
viewController = self.downloadViewController;
else
{
FileDownloader *fileDownloader = [DataDownloadManager existingFileDownloader:cachedGalleryItemName];
if (! fileDownloader)
{
fileDownloader = [[FileDownloader alloc] init];
[fileDownloader loadURL:item.photoURL forFilePath:cachedGalleryItemName withReceipt:nil];
fileDownloader.delegate = nil;
fileDownloader.notificationName = item.contentId;
fileDownloader.queuePriority = NSOperationQueuePriorityNormal;
[[DataDownloadManager sharedInstance].operationQueue addOperation:fileDownloader];
}
}
}
else
{
DownloadViewController *downloadVC = [[DownloadViewController alloc] initWithNibName:#"DownloadViewController" bundle:nil];
downloadVC.delegate = self;
downloadVC.downloadCompleteNotificationName = item.contentId;
downloadVC.asset = item;
downloadVC.backgroundImageFileName = nil;
downloadVC.totalFileSize = nil;
downloadVC.URLString = item.photoURL;
downloadVC.cachedFileName = cachedGalleryItemName;
self.indexOfDownloadInProgress = index;
self.downloadViewController = downloadVC;
viewController = downloadVC;
}
}
return viewController;
}
Now use this function to identify the view controller
-(NSUInteger) indexOfViewController:(UIViewController *)viewController
{
NSUInteger index = nil;
if ([viewController isMemberOfClass:[ImageViewController class]])
{
ImageViewController *currentViewController = (ImageViewController *)viewController;
index = currentViewController.index;
}
else if ([viewController isMemberOfClass:[DownloadViewController class]])
index = self.indexOfDownloadInProgress;
return index;
}
- (UIViewController *)viewController:(UIViewController *)viewController ForBeforeAfter:(NSInteger) beforeAfter
{
NSUInteger index = [self indexOfViewController:viewController];
if (index == NSNotFound)
return nil;
index = index + beforeAfter;
if ([DataDownloadManager sharedInstance].internetNotAvailable)
{
while (index < self.imageArray.count - 1)
{
GalleryItems *item = self.imageArray[index];
if ([item isDownloaded])
break;
index = index + beforeAfter;
}
}
return [self viewControllerAtIndex:index];
}
now do this in page view controller delegate
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
return [self viewController:viewController ForBeforeAfter:-1];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
return [self viewController:viewController ForBeforeAfter:+1];
}
init page view controller like this
- (void)initPageViewController:(UIViewController *)initViewController
{
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[self.pageViewController setDataSource:self];
[self.pageViewController setViewControllers:#[initViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self.pageViewController.view setFrame:self.view.frame];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
[self.view sendSubviewToBack:self.pageViewController.view];
}
in viewDidLoad of the class(in my case it is DisplayImageViewController) you are using this page you can add this tine of code for initialization
[self initPageViewController:[self viewControllerAtIndex:self.index]];
this DisplayImageViewController class is used to display the image you just remove the UIIMAGE to something you want.
and before you push this view controller in navigation set the property like this
DisplayImageViewController *divc = initialize display view controller class; // here you just set the item in array in which you want to implement swipe
divc.imageArray = self.imageArray;
divc.galleryAsset = self.gallery;
divc.index = indexPath.item;
[self presentViewController:divc animated:YES completion:nil];

Swift: SWReavealController Can't Swipe to close menu "only open with swipe"

This is Slideout menu using SWRevealController Which run perfectly but can swipe to open but doesn't swipe to close the menu
After adding the library I did few changes but I did know where is the issue.
This is the code in the main viewcontroller
#IBOutlet weak var menuButton:UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.revealViewController().delegate = self
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
self.revealViewController().delegate = self
var swl = self.revealViewController()
if swl != nil
{
swl.panGestureRecognizer()
swl.tapGestureRecognizer()
}
}
#IBAction func but_back(sender: AnyObject) {
self.navigationController?.interactivePopGestureRecognizer!.delegate = self
self.navigationItem.leftBarButtonItem?.target=self.revealViewController()
self.navigationItem.leftBarButtonItem?.action=Selector("revealToggle:")
self.revealViewController().revealToggle(sender)
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
{
if position == FrontViewPosition.Left // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
{
self.view.userInteractionEnabled = true
revealController.panGestureRecognizer().enabled=true
}
else
{
self.view.userInteractionEnabled = false
revealController.panGestureRecognizer().enabled=false
}
}
It appears you are disabling the pan gesture recognizer, along with disabling user interaction on your view, when the the reveal view controller front position is not at the left position in your delegate method implementation for revealController:willMoveToPosition: from SWRevealViewControllerDelegate. Therefore, the reveal view controller can only move to a single position. You can change that code to allow the additional positions that you require.
For example, if you comment out this code
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
{
if position == FrontViewPosition.Left // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
{
self.view.userInteractionEnabled = true
revealController.panGestureRecognizer().enabled=true
}
else
{
self.view.userInteractionEnabled = false
revealController.panGestureRecognizer().enabled=false
}
}
that should prevent the disabling of the pan gesture unnecessarily.
If you need to disable the reveal view controller pan gesture, I'd suggest using the delegate method revealControllerPanGestureShouldBegin: from SWRevealViewControllerDelegate.

How to make SWRevealViewController menu disappear when users tap on the view-controller

I have View controller with embedded SWRevealViewController I've added code below to disable any interaction while the menu on use.
The viewcontroller is embeded with tableview.
How to make menu disappears when users tap on the view-controller as the slack app "When the menu is on use and you tap on the chat the menu disappears"
class Feed: UIViewController,SWRevealViewControllerDelegate {
#IBOutlet weak var menuButton:UIBarButtonItem!
#IBOutlet weak var menuButton:UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.revealViewController().delegate = self
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
fetchMessages()
}
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
{
if position == FrontViewPosition.Left // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
{
self.view.userInteractionEnabled = true
}
else
{
self.view.userInteractionEnabled = false
}
func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition)
{
if position == FrontViewPosition.Left // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
{
self.view.userInteractionEnabled = true
}
else
{
self.view.userInteractionEnabled = false
}
}
}
//set the delegate in your view controller class
class FeedVC: UIViewController,SWRevealViewControllerDelegate,UIGestureRecognizerDelegate
override func viewDidLoad() {
super.viewDidLoad()
self.revealViewController().delegate = self
var swl=self.revealViewController()
if swl != nil
{
swl.panGestureRecognizer()
swl.tapGestureRecognizer()
}
}
// create the left bar button action
#IBAction func but_back(sender: AnyObject) {
self.navigationController?.interactivePopGestureRecognizer.delegate=self
self.navigationItem.leftBarButtonItem?.target=self.revealViewController()
self.navigationItem.leftBarButtonItem?.action=Selector("revealToggle:")
self.revealViewController().revealToggle(sender)
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
//set the delegate method for SWLReval
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
{
if position == FrontViewPosition.Left // if it not statisfy try this --> if revealController.frontViewPosition == FrontViewPosition.Left
{
self.view.userInteractionEnabled = true
revealController.panGestureRecognizer().enabled=true
}
else
{
self.view.userInteractionEnabled = false
revealController.panGestureRecognizer().enabled=false
}
}
the updated code is available in this link , download the project in here

UINavigationViewControllerDelegate with custom animations break default behavior?

I've been trying to make UINavigationViewControllerDelegate to implement the required methods for custom transitions. They are working as expected and I am also able to add interactive transitions into the mix as well.
The problem is that when I implement those methods I lose the default "swipe right to go back" support from normal navigation transitions completely. I gain those back by setting the navigationController.delegate = nil before entering the view controllers I want to have the normal transitions. This means I'll have to store the actual old delegate and re-set it when I return from the view.
The documentation states that one should return nil from the navigationController:interactionControllerForAnimationController: and navigationController:animationControllerForOperation:fromViewController:toViewController: which is exactly what I am doing:
- (id<UIViewControllerAnimatedTransitioning>)navigationController:
(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
if([fromVC isKindOfClass:[MainViewController class]] &&
[toVC isKindOfClass:[MenuViewController class]]) {
self.menuTransition.isPresentation = YES;
return self.menuTransition;
} else if([toVC isKindOfClass:[MainViewController class]] &&
[fromVC isKindOfClass:[MenuViewController class]]){
self.menuTransition.isPresentation = NO;
return self.menuTransition;
}
return nil;
}
- (id<UIViewControllerInteractiveTransitioning>) navigationController
(UINavigationController *)navigationController
interactionControllerForAnimationController:
(id<UIViewControllerAnimatedTransitioning>)animationController
{
MenuTransition *t = (MenuTransition*)animationController;
if(![t isPresentation] && [t isInteractive]) {
return self.menuTransition;
}
return nil;
}
What else could be wrong here?
The docs do give the impression that returning nil would work, but I found that the gesture recognizers were conflicting. Implementing gestureRecognizerShouldBegin fixed the issue for me.
*Note, this was written in swift but should be easy enough to convert to obj-c.
This is a Navigation Controller SubClass with the UIGestureRecognizerDelegate Protocol
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.interactivePopGestureRecognizer.delegate = self
self.transitionGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
self.view.addGestureRecognizer(transitionGesture)
self.transitionGesture!.delegate = self
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
if self.transitionCoordinator()?.isAnimated() {
return false
}
if self.viewControllers.count < 2 {
return false
}
var currentVC: UIViewController? = self.viewControllers[self.viewControllers.count-1] as? UIViewController
if let vc = currentVC as? MyCustomVC {
if gestureRecognizer == self.transitionGesture {
return true
}
} else if gestureRecognizer == self.interactivePopGestureRecognizer {
return true
}
return false
}
When push viewController2 in viewController1 set navigationController.delegate = nil , then in your pushed view controller interactive pop gesture will be default and work perfectly, and when you pop viewController2
add this code to viewController1
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.delegate = navigationController as? UINavigationControllerDelegate
}

SWRevealViewController: Remove interaction on frontview when rearview is revealed

I need to disable user interaction on front view when rear view is revealed. Found some others asking the same thing but can't really understand where or how to implement the code that I've seen.
Ex: I found this code from link,
- (void)revealController:(SWRevealViewController *)revealController
willMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
- (void)revealController:(SWRevealViewController *)revealController
didMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
Also found few other links
Link1
Link2
Link3
I have this code, but not really sure about the correct place to insert this code. I've tried adding it in my front/rear views and also in the SWRevealViewController method with no success
Appreciate if someone can point me in the right direction.
I've recently come up with a solution that I wanted to share
(sorry if it's 2 months late).
To disable user interaction on the Front View while the Menu is open, I added the following codes on my MenuViewController:
on viewWillAppear:
[self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
and on viewWillDisappear:
[self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
This will disable all user interactions on the Front View Controller, which means that the slide / tap gestures to CLOSE the menu will also be DISABLED.
Now, I have created a ParentViewController and made all the view controllers (the menu items) a subclass of it.
on my viewDidLoad, I put the following codes:
SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
If you run your app at this point, it would appear that the Tap Gesture works (a tap on the Front View will close the Menu), but NOT the Pan Gesture. I'm not sure why this is so, but in order to enable the slide gesture to CLOSE your menu, add the following code in your MenuViewController:
on viewWillAppear:
[self.revealViewController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
To summarize, here's what you need:
On your MenuViewController:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
[self.revealViewController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
}
And on your menu items' view controller (you can make a ParentViewController for all of them):
-(void)viewDidLoad {
[super viewDidLoad];
SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
}
Hope this helps!
I have used another approach to achieve the same outcome not sure if it helps.
Assign SWRevealViewControllerDelegate to AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SWRevealViewController* reveal = (SWRevealViewController*)self.window.rootViewController;
reveal.delegate = self;
// other bootstrapping code
}
and then in the delegate method -(void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position as below:
-(void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
{
if(position == FrontViewPositionLeft){
[revealController.frontViewController.view setUserInteractionEnabled:YES];
[revealController.frontViewController.revealViewController tapGestureRecognizer];
}else{
[revealController.frontViewController.view setUserInteractionEnabled:NO];
}
}
UPDATED: added this line [revealController.frontViewController.revealViewController tapGestureRecognizer] to close the revealed controller when tap on frontviewcontroller
Swift version to #hardluckbaby answer:
In MenuViewController(Rear view controller):
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.revealViewController().frontViewController.view.userInteractionEnabled = false
self.revealViewController().view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.revealViewController().frontViewController.view.userInteractionEnabled = true
}
In FrontViewController(You can make a ParentViewController for all of your front view controllers as #hardluckbaby said):
override func viewDidLoad() {
super.viewDidLoad()
if let revealController = self.revealViewController() {
revealController.panGestureRecognizer()
revealController.tapGestureRecognizer()
}
}
As John Explained: Although these solutions do work, I don't think any of them address the original question, which is actually quite simple:
There are 2 steps involved:
Add the following methods to your FrontViewController.m:
(void)revealController:(SWRevealViewController *)revealController
willMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
(void)revealController:(SWRevealViewController *)revealController
didMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
Make your front view controller be a delegate of SWRevealViewController in the FrontViewController.h file:
(e.g. #interface HomeViewController : UIViewController )
where my FrontViewController was named HomeViewController
and also in the FrontViewController.m file with the following on ViewDidLoad:
self.revealViewController.delegate = self;
Problem solved! Much easier than creating parent classes, etc.
This will help you solve the user interactions for the FrontView controller lovely I would just add the following change taken from Xun's response also above and you will solve both the user interactions and the hide menu when user taps FrontViewController.
- (void)revealController:(SWRevealViewController *)revealController
willMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
- (void)revealController:(SWRevealViewController *)revealController
didMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
//Hides the menu when user taps FrontViewController
[revealController.frontViewController.revealViewController tapGestureRecognizer];
}
}
Swift 3.0 simple and fast method.
Frontviewcontoller code here...
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
let rewel:SWRevealViewController = revealViewController()
rewel.panGestureRecognizer()
rewel.tapGestureRecognizer()
}
}
SideDrowerviewcontoller code here...
override func viewWillAppear(_ animated: Bool) {
let rewel = self.revealViewController()
rewel?.frontViewController.view.isUserInteractionEnabled = false
rewel?.frontViewController.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func viewWillDisappear(_ animated: Bool) {
let rewel = self.revealViewController()
rewel?.frontViewController.view.isUserInteractionEnabled = true
}
Add a subview to front view when rear view is open.
In viewWillAppear method of your menu items controller, Just create an overlay button on the front view and set action to revealToggle: of revealViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
overlayView = [UIButton buttonWithType:UIButtonTypeCustom];
overlayView.frame = self.revealViewController.frontViewController.view.bounds;
overlayView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.8];
overlayView.tag = 999;
[overlayView addTarget:self.revealViewController action:#selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
[overlayView addTarget:self.revealViewController action:#selector(revealToggle:) forControlEvents:UIControlEventTouchDragOutside];
[self.revealViewController.frontViewController.view addSubview:overlayView];
}
In revealTogglle method remove the overlay button if any:
- (void)revealToggleAnimated:(BOOL)animated
{
UIButton *overlayView = (UIButton*)[self.view viewWithTag:999];
if (overlayView) {
[overlayView removeFromSuperview];
overlayView = nil;
}
// rest of the code...
}
Although these solutions do work, I don't think any of them address the original question, which is actually quite simple:
There are 2 steps involved:
1) Add the following methods to your FrontViewController.m:
- (void)revealController:(SWRevealViewController *)revealController
willMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
- (void)revealController:(SWRevealViewController *)revealController
didMoveToPosition:(FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
2) Make your front view controller be a delegate of SWRevealViewController in the FrontViewController.h file:
(e.g. #interface HomeViewController : UIViewController <SWRevealViewControllerDelegate>)
where my FrontViewController was named HomeViewController
and also in the FrontViewController.m file with the following on ViewDidLoad:
self.revealViewController.delegate = self;
Problem solved! Much easier than creating parent classes, etc.
Another way is to have an overlay view when the rear view is revealed. You can use this updated library https://github.com/NSRover/SWRevealViewController and make sure you include shouldUseFrontViewOverlay = true when the rear view is revealed.
class SideMenuViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.revealViewController().delegate = self
}
}
extension SideMenuViewController: SWRevealViewControllerDelegate {
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
if position == .Left {
revealController.frontViewController.view.userInteractionEnabled = true
}
if position == .Right {
revealController.frontViewController.view.userInteractionEnabled = false
}
}
}
On MenuTableViewController/ Rear VC, add SWRevealViewControllerDelegate.
override func viewDidLoad() {
super.viewDidLoad()
self.revealViewController().delegate = self
if self.revealViewController() != nil {
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
Add this delegate method.
func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) {
if(position.rawValue == 4)
{
//move to rear
self.revealViewController().frontViewController.view.userInteractionEnabled = false
}
else if (position.rawValue == 3)
{
//move to front - dashboard VC
self.revealViewController().frontViewController.view.userInteractionEnabled = true
}
}
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
//will perform the same function as the above delegate method.
}
Consider following solution, works perfect
private let DimmingViewTag = 10001
extension UIViewController: SWRevealViewControllerDelegate {
func removeInteractionFromFrontViewController() {
revealViewController().delegate = self
view.addGestureRecognizer(revealViewController().panGestureRecognizer())
}
//MARK: - SWRevealViewControllerDelegate
public func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) {
if case .Right = position {
let dimmingView = UIView(frame: view.frame)
dimmingView.tag = DimmingViewTag
view.addSubview(dimmingView)
view.bringSubviewToFront(dimmingView)
} else {
view.viewWithTag(DimmingViewTag)?.removeFromSuperview()
}
}
}
Simple usage in UIViewController:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
removeInteractionFromFrontViewController()
}
Addition to hardluckbaby answer.
If you run your app at this point, it would appear that the Tap
Gesture works (a tap on the Front View will close the Menu), but NOT
the Pan Gesture. I'm not sure why this is so, but in order to enable
the slide gesture to CLOSE your menu, add the following code in your
MenuViewController:
on viewWillAppear:
[self.revealViewController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
It adds some undesired behavior, e.g. pan gesture will close rear view when starts on it.
Default pan gesture may not work if you add it to your own view somewhere, something like on viewDidLoad of your front view controller:
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
Remove such lines and this should works as expected, for pan and tap gestures
SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
I was using the viewWillAppear and viewWillDisappear functions but as I have subviews for almost every item in the side menu I had. My issue was that I had a input field active (keyboard displaying) and accessed the side menu. In the root menu the keyboard hid but after I entered a submenu keyboard showed up again. To solve this I changed the approach to enable and disable the interaction in revealController like this:
- (void)revealController:(SWRevealViewController *)revealController
didMoveToPosition:(FrontViewPosition)position {
if (position == FrontViewPositionRight) {
[self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
} else if (position == FrontViewPositionLeft) {
[self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
}
}
Firstly just set your delegate :
self.revealViewController.delegate = self;
and the delegate method are given below :
- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
{
static NSInteger tagLockView = 123456789;
if (revealController.frontViewPosition == FrontViewPositionRight)
{
UIView *lockView = [self.view viewWithTag:tagLockView];
[UIView animateWithDuration:0.3 animations:^{
lockView.alpha = 0;
} completion:^(BOOL finished) {
[lockView removeFromSuperview];
}];
}
else if (revealController.frontViewPosition == FrontViewPositionLeft)
{
UIView *lockView = [[UIView alloc] initWithFrame:self.view.bounds];
lockView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
lockView.tag = tagLockView;
lockView.backgroundColor = [UIColor blackColor];
lockView.alpha = 0;
[lockView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self.revealViewController action:#selector(revealToggle:)]];
[self.view addSubview:lockView];
[UIView animateWithDuration:0.3 animations:^{
lockView.alpha = 0.5;
}];
}
}

Resources