2

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.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
anuj
  • 261
  • 5
  • 15
  • Welcome on SO, please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask). Do not forget to provide some code as [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and format your question properly, using the [provided markdown options](https://stackoverflow.com/editing-help). – jhoepken Sep 18 '17 at 06:57
  • Share your concerned code. how you're implemented AMSlideMenu for one viewController? – Muhammad Umair Sep 18 '17 at 07:11
  • @NSIceCode no i have not followed that i am new so i need step by step or if code is available – anuj Sep 18 '17 at 07:14

2 Answers2

9

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)
Amir Khan
  • 1,318
  • 1
  • 14
  • 39
0

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

Muhammad Umair
  • 1,664
  • 2
  • 20
  • 28
  • my login page is not with menu ,its a second view controller in which slide menu is there @NSIceCode – anuj Sep 18 '17 at 07:17
  • You can go through to these tutorials, both provided sample code at the end, which can give you a good idea and you can use it according to your need. – Muhammad Umair Sep 18 '17 at 07:21
  • could u create a sample for ur side as per my requirement @NSIceCode i all appreciate ur help – anuj Sep 18 '17 at 07:22
  • Here on SO we can only guide you with some tutorials or can look into your written code for bugs/errors. Show me if you have tried something? – Muhammad Umair Sep 18 '17 at 07:29
  • i have no idea how to do – anuj Sep 18 '17 at 07:29