-1

I am using Swift 3 with SWRevealViewController to create side bar menu app.

How do I control the color of front view when rear view (menu) is open? I would like to display gray color (like disabled/ transparent). So far whatever I tried hasn't worked and not able to find straight forward solution online. I could control rearview color but not front view.

So far I have tried changing front view color in viewWillAppear of rear view controller.

viewWillAppear in MenuController looks like:

override func viewWillAppear(_ animated: Bool) {

print("MenuController - viewWillAppear")
super.viewWillAppear(animated)
self.revealViewController().frontViewController.view.isUserInteractionEnabled = false
self.revealViewController().view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.revealViewController().frontViewController.view.backgroundColor = UIColor.blue
self.revealViewController().rearViewController.view.backgroundColor = UIColor.gray
}

It changes color for rear view but doesn't work for front view. And I want grayed out disabled look not just change background color.

SRM
  • 43
  • 7

1 Answers1

0

Try and add this to your viewWillAppear method in your rearVieController class

revealViewController.frontViewController.view.backgroundColor = UIColor.blue //Set colour to what ever you want

So it should look like this

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    revealViewController.frontViewController.view.backgroundColor = UIColor.blue
}

Edit:

Okay I have tried to do this and it seems to work

If you create a new UIView and add it to the frontViewController then remove it when the view disappears.

var view: UIView?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //Create the view the same size as your frontViewController
    view = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(revealViewController.view.frame.size.width), height: CGFloat(revealViewController.view.frame.size.height)))
    //Set the colour of the view to whatever you like
    view?.backgroundColor = UIColor.blue
    //Add the new view to the frontViewcontroller
    revealViewController.frontViewController.view.addSubview(view!)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    //Remove the view from the frontViewController
    view?.removeFromSuperview()
}

Edit:

If you add a tap gesture to the UIView then remove the view in the tap function instead of the viewWillDisappear

var view: UIView?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //Create the view the same size as your frontViewController
    view = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(revealViewController.view.frame.size.width), height: CGFloat(revealViewController.view.frame.size.height)))
    //Set the colour of the view to whatever you like
    view?.backgroundColor = UIColor.blue
    //Add a tap gesture to the UIview
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    view?.addGestureRecognizer(tap)
    //Add the new view to the frontViewcontroller
    revealViewController.frontViewController.view.addSubview(view!)
}

func handleTap(_ sender: UITapGestureRecognizer) {
    //Remove the view from the frontViewController
    view?.removeFromSuperview()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    //Moved the remove to the tap gesture function
}
Luke Chase
  • 322
  • 1
  • 18
  • viewWillAppear in MenuController looks like: override func viewWillAppear(_ animated: Bool) { print("MenuController - viewWillAppear") super.viewWillAppear(animated) self.revealViewController().frontViewController.view.isUserInteractionEnabled = false self.revealViewController().view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) self.revealViewController().frontViewController.view.backgroundColor = UIColor.blue self.revealViewController().rearViewController.view.backgroundColor = UIColor.gray } – SRM Mar 29 '17 at 13:27
  • Your suggestion helped me move forward. In RearViewController viewWillAppear and viewWillDisappear, I am adding and removing subview of FrontViewController but one small issue. I want to subView to be gone as soon as you tap on it, right now it stays till the front view controller screen shows up completely. How do I recognize tap or pan gesture and remove subview before the menu starts sliding on left. Thank you very much for your help. – SRM Mar 30 '17 at 13:26
  • Iv updated my answer which will hopefully solve your problem. – Luke Chase Mar 30 '17 at 14:09
  • Thanks Luke for your guidance. I delegated willMoveTo function of SWRevealViewController and removed subview there. I guess that's ok. also I found out that I could create subview by doing this way too: view = UIView(frame: revealViewController.view.frame). Cheers!!! – SRM Mar 30 '17 at 14:31
  • Thats okay glad to help :) – Luke Chase Mar 30 '17 at 15:01