fogwolf
fogwolf

Reputation: 941

How create hidden sliding menu view for iOS that is always available from navigation controller

I know this topic has been somewhat covered, but I'm not seeing exactly what I need posted. Apologies if I'm missing it. Basically I want to have something very similar to the Facebook slide-out navigation menu. I've seen the posting here which points to using ViewDeck, but for various reasons I would like to roll my own. My app is based around a navigation controller, and I plan to have a button on my nav bar which toggles the visibility of the menu view.

My question here is not how to create and manage such a view in general, but more specifically, how could I create a view controller for this view and have it always present and available, no matter what view controller is currently on top of my navigation controller's stack? I'm using storyboards and my first thought was to add a container view to my main navigation controller and adding the menu view there, but as I suspected, the storyboard editor will not allow me to directly add any views to my navigation controller. I know this makes sense, no need to explain to me why I'm unable to do this, including this more to show my point that I would like to do something like this - create it at the top-most global level so it's always there without having to be created by each view controller in my app.

I hope my question is clear. Just in case, let me illustrate my thinking - if I added a container view to my initial root view controller that I managed hiding and sliding out from the side, once I navigated away from my root view controller by pushing a new view controller on my nav stack, this menu view would no longer be there. Of course I do not want to be required to subsequently create it for every distinct view controller in my app.

Any suggestions or help greatly appreciated.

Upvotes: 0

Views: 3899

Answers (4)

Richard C.
Richard C.

Reputation: 126

I actually just finished a project that did exactly what your wanting to do.

Here's the steps I took.

  • Made a view controller
  • Added menu navigation on the left
  • Add a UIView over the menu navigation
  • Added IBActions for sliding left and right with animations like this...

    [UIView animateWithDuration:.25f delay:0.0 options:0
                 animations:^{
                     [mainView setFrame:CGRectMake(0, mainView.frame.origin.y, mainView.frame.size.width, mainView.frame.size.height)];
    
                 }
                 completion:^(BOOL finished){
    }];
    
  • Added additional views inside that same view controller

So pretty much your one main view controller holds all of your views and you just animate them to slide left and right with either a button or UIGestureRecognizers. You can use a Container View to nest child view controllers inside that main view controller to make the code side easier to manage.

Upvotes: 1

fogwolf
fogwolf

Reputation: 941

Thanks for the suggestions. I don't want to use a 3rd party library, in part because I do not want to have the exact same behavior. For example, rather than the side menu pushing the main view out of the way, I want the menu to appear over the content view. I also want it to appear underneath the nav bar. Plus I am not using a right menu, so it's possible for me to have lighter code to handle what it is I need.

I think I've come up with a solution. I have created a menu view controller and added a reference to this in my app delegate. Now wherever the user is, when he or she clicks the menu button, it calls a method on the app delegate to hide or show the menu. So the app delegate handles the animation and adding & removing the menu as a subview of the currently displayed view controller.

Upvotes: -2

alloc_iNit
alloc_iNit

Reputation: 5183

Well,I'm not sure my suggestion is the exactly same that you are looking for and mentioned above. But let me try & draw you a visual sketch for you.

  1. Create a menuview as UIViewController and set design as you desire.
  2. Implement design & logic for your menu architecture in the same view with Hide & Show methods which will be react to respective delegates. Now, your menuview is ready as an ordinary view that generally we can push and pop to navigate. Now, the main show begins.
  3. As your app is having a UINavigationController, it will be having a custom toolbar containing a UIButton to welcome menuview.
  4. Now, a singleton instance of menuview will be created to manage a common menu action for entire app and the most importantly it will be added on UIWindow.
  5. And this singleton menuview instance will request for Show/Hide as per requirement from the custom toolbar button.
  6. On request of Show/Hide, apply respective animations with expected logic.
  7. While requesting Hide, menuview will be removed from the UIWindow and next time when it requires to Show add it again on UIWindow.

Hope, this might have work for you. In case of any misconception let me know.

Upvotes: 2

CainaSouza
CainaSouza

Reputation: 1437

You can use some open source controls available like JASidePanels (here).

Upvotes: 1

Related Questions