How to configure SWRevealViewController Programmatically?

ios swift xcode swrevealviewcontroller residemenu

517 просмотра

1 ответ

0 Репутация автора

I am working on application where after registration process home screen will appear. And the SWRevealViewController has been configure in home screen.

I want to configure SWRevealViewController programmatically.

Below is my code

let storyboard = UIStoryboard(name: "MainSW", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
        let rvc:SWRevealViewController = self.revealViewController() as SWRevealViewController
        rvc.pushFrontViewController(vc, animated: true)

But it gives me error like below.

Could not cast value of type 'UINavigationController' (0x110d9f860) to 'SWRevealViewController' (0x10dc346b8).

I have tried below link also

SWRevealViewController solution

But it also doesn't help me.

Автор: user3432164 Источник Размещён: 18.07.2016 08:00

Ответы (1)

0 плюса

0 Репутация автора

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 ! = .plain = revealViewController()
      leftButton.action = #selector(SWRevealViewController.revealToggle(_:))

      rightButton.image = UIImage(named: "yourImageName")! // check that the image exists, don't use ! = .plain = revealViewController()
      rightButton.action = #selector(SWRevealViewController.rightRevealToggle(_:))


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

        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.

Автор: user7219266 Размещён: 16.01.2019 10:28
Вопросы из категории :