SWRevealViewController from the right side on swift

风格不统一 提交于 2019-12-04 01:21:40

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.

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

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:")

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

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
 *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.**

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!