sinusGob
sinusGob

Reputation: 4313

revealViewController delegate doesn't work

I'm using SWReveal library to create the side menu, My goal is whenever a user click the menu bar, then it will open the side menu and disable the front view and will make the front view black color with opacity.

I need to press the menu burger bar to go back to the front view

Example that I'm trying to emulate

enter image description here

My code

import UIKit

class ViewController: UIViewController, SWRevealViewControllerDelegate {

@IBOutlet var menuBarBurger: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()
    revealViewController().delegate = self
    setupSideMenu()
}

func setupSideMenu() {
    if revealViewController() != nil {
        menuBarBurger.target = self.revealViewController()
        menuBarBurger.action = #selector(SWRevealViewController.revealToggle(_:))
        revealViewController().rearViewRevealWidth = 300

        view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
}

// This is the part where the background will turn blur and so on.
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {

    let tagId = 8009
    if revealController.frontViewPosition == FrontViewPosition.right {
        let lock = self.view.viewWithTag(tagId)
        UIView.animate(withDuration: 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.black
        lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:))))
        self.view.addSubview(lock)

        UIView.animate(withDuration: 0.5, animations: {
            lock.alpha = 0.333
        })
    }
}
}

The storyboard

enter image description here

The tagid (just in case if you are asking)

enter image description here

What should I do, to achieve the same design?

Upvotes: 0

Views: 302

Answers (1)

NSAdi
NSAdi

Reputation: 1253

You haven't set the delegate to self

revealViewController().delegate = self

Assuming that revealViewController() is of type SWRevealViewController and is the ViewController you want to show.

Upvotes: 1

Related Questions