问题
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
回答1:
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.
回答2:
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
回答3:
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:")
回答4:
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
回答5:
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
回答6:
*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.**
来源:https://stackoverflow.com/questions/30353080/swrevealviewcontroller-from-the-right-side-on-swift