12

I am using SWRevealViewController library to make a slide out menu in swift, but I am struggling to put this menu in the right side. I have seen on the library description this comment

// Optional right view controller, can be nil if not used
@property (nonatomic) UIViewController *rightViewController;

but I am not familiar with objective C and I am not being able to do this on swift. This is a part of my code for the default(left) menu:

class ViewController: UIViewController, AVAudioPlayerDelegate {

@IBOutlet weak var playlistOutlet: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    playlistOutlet.target = self.revealViewController()
    playlistOutlet.action = Selector("revealToggle:")

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

}
}

And revealViewController is called from the SWRevealViewController library

Ilir V. Gruda
  • 1,562
  • 5
  • 17
  • 23
  • Add more code to your question. What is playlistOutlet? How do you declare revealViewController? – DilumN May 20 '15 at 14:53

6 Answers6

25

I followed this solution, in the controller of the front siding view use this:

override func viewDidLoad() {
    super.viewDidLoad()

    if self.revealViewController() != nil {
        favoritesButton.target = self.revealViewController()
        favoritesButton.action = "rightRevealToggle:"
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
}

Don't forget to create the Outlet to the item bar button first. In the storyboard, set the segue value to sw_right

Xcode 7 Beta 6 Screenshot

And first that didn't work for me, but a clean helped.

Michael K.
  • 535
  • 6
  • 21
7

Swift 3

Add this code to yourViewController

if revealViewController() != nil{
        sideMenuBtn.target = revealViewController()
        sideMenuBtn.action = #selector(SWRevealViewController.rightRevealToggle(_:))
        view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

at MainStoryBoard

Set the segue identifier value to sw_right instead of sw_rear

  • Worked for me

Better Alternative: https://github.com/jonkykong/SideMenu

  • Set the segue identifier value to sw_right instead of sw_rear => Worked for me. Thanks!! – Ravi Apr 26 '18 at 06:57
  • I Have a problem, I want to control the opening of the side menu according to a condition , when condition is true open on left , and when false open on right , how can we set this independently from using the storyboard segues – MhmdRizk May 22 '18 at 10:50
  • @MhmdRizk you can make two view controllers in the main story board or two separated main story boards and load the one that fit your needs but If I were you, I'd make it programmatically better – Mamdouh El Nakeeb May 22 '18 at 15:38
  • @MhmdRizk were you able to find the solution? – Ashish Sharma Aug 09 '18 at 05:47
  • @AshishSharma no, I simply stoped using SWReveal, and I recommend you too to stop using it. and this is a great alternative that is better more customizable : https://github.com/teodorpatras/SideMenuController – MhmdRizk Aug 09 '18 at 06:49
  • @MhmdRizk okay, for anyone else looking at this solution please look at my answer below. – Ashish Sharma Aug 09 '18 at 10:59
  • 1
    This is a great solution: https://github.com/jonkykong/SideMenu. Thanks @MamdouhElNakeeb – KSR Dec 19 '21 at 06:38
6

Can you try this code:

    var storyboard = UIStoryboard(name: "Main", bundle: nil)
    var sidemenuViewController = storyboard.instantiateViewControllerWithIdentifier("sideMenu") as! SideMenuViewController
    sidemenuViewController.delegate = self
    revealViewController().rightViewController = sidemenuViewController
    revealViewController().delegate = self
    sideMenuItem.target = self.revealViewController()
    self.revealViewController().rightViewRevealWidth = self.view.frame.width * 0.8
    sideMenuItem.action = Selector("rightRevealToggle:")
ridvankucuk
  • 2,407
  • 1
  • 23
  • 41
  • actually I am having many bugs, like in the second line -Used of undeclared type 'SideMenuViewController'. In the 5th line -Can not assign a value of type 'ViewController' to value of type 'SWRevealViewControllerDelegate'... – Ilir V. Gruda May 20 '15 at 15:44
  • You should import SWRevealViewControllerDelegate first of all. Are you using Storyboard? – ridvankucuk May 20 '15 at 15:45
  • 1
    yes I am using Storyboard. And I think I found a solution. In the Storyboard Segue Identifier I am adding sw_right and than I am changing this line playlistOutlet.action = Selector("rightRevealToggle:") but now I am not being able to see the full width of the menu, because the left side of the menu is hided from the main view. Is there a way to move the menu in the right side ? – Ilir V. Gruda May 20 '15 at 15:52
  • I have edited my answer and tried the code in my project. It works now. – ridvankucuk May 20 '15 at 20:10
  • Sorry for bothering you so much @ridvankucuk but again I am having a problem. I am pasting your code implementing SWRevealViewControllerDelegate in the main ViewController and than in second line I am implementing the var as TableViewController, but in third line I am having an error saying 'TableViewController' dose not have a member named 'delegate' – Ilir V. Gruda May 21 '15 at 12:17
  • You can delete that line from your code. Do you get any other error? – ridvankucuk May 21 '15 at 12:26
  • Now I am not having errors before building, but when I am running the app it is crashing with an error 'libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)' and it is pointing at the AppDelegate class – Ilir V. Gruda May 21 '15 at 12:37
1

Swift 4.1

private func setRightMenu(){
    btnMenuright.addTarget(revealViewController(), action: #selector(SWRevealViewController.rightRevealToggle(_:)), for: UIControlEvents.touchUpInside)

}

I tried to do this when you want the sw_right show up. this code It worked for me

Sup.Ia
  • 277
  • 2
  • 8
0

For menu from right side,

menuButton.action = #selector(SWRevealViewController.rightRevealToggle(_:))

For those who are looking for localization and support for RTL/LTR Languages.

All you need to do is, when you create a new localization file, select the option "Interface builder Storyboard" instead of "Localizable string". It will create another storyboard for that language, and it's okay to do this cuz as far as I know, you'll use it only for Arabic language.

Now in the new storyboard rename the segue to "sw_right" and in the old one let it be "sw_rear".

When in your viewDidLoad() for the menu button, simply check for the current locale language

let language = NSLocale.preferredLanguages[0] , and open menu from either side depending on your choice.

This is my piece of code:

if self.revealViewController() != nil {

            menuButton.target = self.revealViewController()

            let language = NSLocale.preferredLanguages[0]

            if language == "ar-AE" || language == "ar-US" || (language.range(of: "ar-") != nil) {
                menuButton.action = #selector(SWRevealViewController.rightRevealToggle(_:))
            }
            else {
                menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            }

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

        }

You may also use the region code as I've noticed that the user may change language or the region individually.

let region = NSLocale.current.regionCode
Ashish Sharma
  • 663
  • 5
  • 15
0
 *override func viewDidLoad() {
  super.viewDidLoad()
.
.
.
 menuButton.addTarget(revealViewController(), action: #selector(SWRevealViewController.rightRevealToggle(_:)), for: .touchUpInside)
  if revealViewController() != nil {
   revealViewController().rightViewRevealWidth = 200 //any number
   view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()
   view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
        }
    }
}

in the main story board add a segue of type "custom" from SWRevealViewController to ur sideMenuViewController then select the segue and in the inspector set its identifier to sw_right and class to SWRevealViewControllerSegueSetController that's all.**

Mahgolsadat Fathi
  • 3,107
  • 4
  • 16
  • 34