Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWRevealViewController from the right side on swift

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

like image 623
Ilir V. Gruda Avatar asked May 20 '15 14:05

Ilir V. Gruda


4 Answers

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

like image 84
Mamdouh El Nakeeb Avatar answered Nov 09 '22 23:11

Mamdouh El Nakeeb


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:")
like image 44
ridvankucuk Avatar answered Nov 10 '22 00:11

ridvankucuk


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.

like image 25
Michael K. Avatar answered Nov 10 '22 00:11

Michael K.


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

like image 1
Sup.Ia Avatar answered Nov 10 '22 00:11

Sup.Ia