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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Hi, when you downloads the SWRevealController from github, it have examples folder & examples sotryboard folder. in the example folder they have a project where they implemented swreveal programatcally! – Mohamad Bachir Sidani Jul 18 '16 at 08:20
  • They have code for navigation from one view to another view .I want to configure swrevealviewcontrtoller programmatically –  Jul 18 '16 at 08:25
  • they have how thyey set up svprogresshud programatically. take alook at the appDelegate I think it is there. – Mohamad Bachir Sidani Jul 18 '16 at 08:34
  • here is a link of the app delegate https://github.com/John-Lluch/SWRevealViewController/blob/master/RevealControllerExample/RevealControllerProject/AppDelegate.m – Mohamad Bachir Sidani Jul 18 '16 at 08:35
  • @BashirSidani I want code in swift not in objective c –  Jul 18 '16 at 09:21
  • oh sorry, i only know the objective c way of this. However you can easily convert it to swift thrugh this link: https://objectivec2swift.com/ you paste an Obj-C code and it convert it to swift code – Mohamad Bachir Sidani Jul 18 '16 at 10:38

1 Answers1

1

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.