How to create Side Menu on right side in Swift 3.0?

9.2k views Asked by At

I am a beginner and I wanted to create a side menu on the right side.

So far I have a UIButton on right top side of viewController, what I want is when I click that button I want to show/hide slide menu with say 3 items…

when I click each item it will go to the different view controller. In my project, i am showing slide menu in only one viewController using AMSlideMenu. Thanks in advance.

3

There are 3 answers

0
Amir Khan On BEST ANSWER

Anuj just follow the steps-

  1. Create a SideMenuViewController which is sub class of UIViewController , using storyboard how it will look according to the requirement.
  2. Add this SideMenuViewController and its view as a child view controller in parent view controller by UIButton click.
  3. When you done, remove SideMenuViewController from parent View controller and remove its view from parent view.

Repeat 2 and 3 for all view controllers.

Updated code :

Declare in your view controller -

var sideMenuViewController = SideMenuViewController()
var isMenuOpened:Bool = false

In viewDidLoad

    sideMenuViewController = storyboard!.instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
    sideMenuViewController.view.frame = UIScreen.main.bounds

In your button Clicked event -

  func openAndCloseMenu(){

    if(isMenuOpened){

        isMenuOpened = false
        sideMenuViewController.willMove(toParentViewController: nil)
        sideMenuViewController.view.removeFromSuperview()
        sideMenuViewController.removeFromParentViewController()

    }
    else{

        isMenuOpened = true
        self.addChildViewController(sideMenuViewController)
        self.view.addSubview(sideMenuViewController.view)
        sideMenuViewController.didMove(toParentViewController: self)
     }

}

For Animation:

let transition = CATransition()

let withDuration = 0.5

transition.duration = withDuration
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft

sideMenuViewController.view.layer.add(transition, forKey: kCATransition)
5
Muhammad Umair On

There is no built-in control in iOS for side-menu. However, you could use different open source libraries to achieve your goal.

Have a look at the following libraries:

https://github.com/John-Lluch/SWRevealViewController

The detailed tutorial for this library:

  1. http://www.appcoda.com/ios-programming-sidebar-navigation-menu/

  2. http://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path

0
iOS Trainingtamil On

example:

extension SideMenuVC: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SideMenuTVCell", for: indexPath) as! SideMenuTVCell
        return cell
    }
}