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
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
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:")
*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.**
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
Swift 3
if revealViewController() != nil{
sideMenuBtn.target = revealViewController()
sideMenuBtn.action = #selector(SWRevealViewController.rightRevealToggle(_:))
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
Set the segue identifier value to sw_right instead of sw_rear
Better Alternative: https://github.com/jonkykong/SideMenu
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
And first that didn't work for me, but a clean helped.