Here is how I have configured it :
I have created 2 UIBarButtonItem
as properties of the HomeViewController
(I will set them in code)
let leftButton = UIBarButtonItem()
let rightButton = UIBarButtonItem()
In viewDidLoad method:
revealViewController().delegate = self
In viewWillAppear method:
if (revealViewController() != nil) {
revealViewController().rearViewRevealWidth = -50
revealViewController().rightViewRevealWidth = -50
leftButton.image = UIImage(named: "yourImageName")! // check that the image exists, don't use !
leftButton.style = .plain
leftButton.target = revealViewController()
leftButton.action = #selector(SWRevealViewController.revealToggle(_:))
rightButton.image = UIImage(named: "yourImageName")! // check that the image exists, don't use !
rightButton.style = .plain
rightButton.target = revealViewController()
rightButton.action = #selector(SWRevealViewController.rightRevealToggle(_:))
view.addGestureRecognizer(revealViewController().panGestureRecognizer())
view.addGestureRecognizer(revealViewController().tapGestureRecognizer())
}
This is all the code I use to set up SWRevealViewController in my HomeViewController in order to have left and right menu.
You can now use the delegate method to customize the behavior as it pleases you :
extension HomeViewController: SWRevealViewControllerDelegate {
func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {
view.alpha = position == FrontViewPosition.left ? 1.0 : 0.6
}
}
I have set an alpha on the HomeViewController to make it darker when it's not in front.
For the Storyboard part :
- Create an UIViewController in the Storyboard.
- Change its class to SWRevealViewController
- Create 2 more UIViewControllers (1 for the left menu, 1 for the right menu. If you want only 1 menu, apply for either left or right, as you want)
- Link the SWRevealViewController to the 2 UIViewControllers with segues (
rear
and right
segues)
- Link the SWRevealViewController to your UINavigationController with the
front segue
.
- Link the UINavigationController with your HomeViewController (with
root relationship
)
Keep in mind that you may also want to create a custom segue for the transition between your RegisterViewController and the HomeViewController.
You can create it programmatically too:
class RegisterToHomeSegue: UIStoryboardSegue {
override func perform() {
let source = self.source as! RegisterViewController
let homeVC = self.destination as! HomeViewController
let window = source.view.window
window?.rootViewController = homeVC
window?.addSubview(source.view)
UIView.transition(from: source.view, to: homeVC.view, duration: 0.50, options: .transitionCrossDissolve, completion: nil)
}
}
And in the Storyboard
change the class segue from your RegisterViewController
to the SWRevealViewController
to this new one RegisterToHomeSegue
and give it an identifier. So that in the RegisterViewController
you can call :
performSegue(withIdentifier: "yourIdentifier", sender: nil)
And voilà
, you now have :
- Nice transition between your RegisterViewController and your HomeViewController
- Your SWRevealController setup properly, with its left and right menu
- Left and Right button item with tap gesture to open the corresponding menu
- Change alpha on your HomeViewController when it is open
Tell me if it is helping you.