Brandon Wilcox
Brandon Wilcox

Reputation: 17

How to Disable Interaction in Sidebar Menu using SWRevealViewController

I followed the Sidebar Menu Tutorial from AppCoda.com and I'm having an issue with it. I want to disable user interaction when the user is in the menu. Right now, the user is still able to interact with the main screens despite being in the menu.

Link to Screenshot of Problem: http://i.imgur.com/1gld2bY.gifv

Upvotes: 0

Views: 717

Answers (2)

Munib
Munib

Reputation: 967

Put this code in your TableViewController i.e. the menu item controller. Essentially this is the view controller that will sit behind your main view.

This also makes your main view go dark, if you don't want that, then set the alpha component to 0.

let darkView = UIView()

override func viewWillAppear(_ animated: Bool) {

    darkView.addGestureRecognizer(revealViewController().tapGestureRecognizer())
    darkView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
    darkView.frame = self.revealViewController().frontViewController.view.bounds
    self.revealViewController().frontViewController.view.addSubview(darkView)
}

override func viewWillDisappear(_ animated: Bool) {
    darkView.removeFromSuperview()
}

Upvotes: 2

Sivajee Battina
Sivajee Battina

Reputation: 4174

If you are using SWRevealViewController for sidebar. Use below code

func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
let tagId = 4207868622

if revealController.frontViewPosition == FrontViewPosition.Right {
    let lock = self.view.viewWithTag(tagId)
    UIView.animateWithDuration(0.25, animations: {
            lock?.alpha = 0
        }, completion: {(finished: Bool) in
            lock?.removeFromSuperview()
        }
    )
    lock?.removeFromSuperview()
} else if revealController.frontViewPosition == FrontViewPosition.Left {
    let lock = UIView(frame: self.view.bounds)
    lock.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    lock.tag = tagId
    lock.alpha = 0
    lock.backgroundColor = UIColor.blackColor()
    lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: "revealToggle:"))
    self.view.addSubview(lock)
    UIView.animateWithDuration(0.75, animations: {
            lock.alpha = 0.333
            }
        )
    }
}

Note: don't forget to add self.revealViewController().delegate = self in your view controller, otherwise the delegate won't be called

Upvotes: 0

Related Questions