Question

I am developing a navigation based application in iPhone.

In which I want to create physical menu button like menu like the one in Android phones.

The menu can be accessed from any view in the hierarchy.

Do any one have any idea how can we achieve this .

Can we add a drawer like menu to UINavigationBar ?

Please don't suggest the tabbarcontroller as it is the last option.

Was it helpful?

Solution

What you should do is create a subclass of UIViewController, e.g. MasterViewController. Have all of the view controllers subclass MasterViewController. In the MasterViewController you can write code to create and display your drawer view, and then all of your view controllers will inherit that code and can call it on themselves at any point.

To slide the view in:

drawerView.frame = CGRectMake(0, -1*drawerView.frame.size.height, self.view.frame.size.width, whateverHeight);
[UIView beginAnimations:nil context:NULL];
drawerView.frame = CGRectMake(0, 0, self.view.frame.size.width, whateverHeight);
[UIView commitAnimations];

OTHER TIPS

I think that what you want is just set the titleView of UINavigationItem to an arbitrary UIView to handle user taps.

This doesn't have to be a text label :)

If that's the case then I'd suggest you taking sharedInstance with flags tracking the hierarchy of the view controller stack. Every time when user navigate just update your sharedInstance and do the custom menu preparation and populate it. The good practice would be reuse your UI component rather than making it new on every navigation operation.

You can create a "DrawerView" class and import it into where you will want to use it. Then you can use presentModalViewController: myDrawer to make it appear when you need it. Hope that makes sense.

Its very simple..

In one XIB u can add many views right. now add two main views like say data view and down side button view u want in. (if m nt wronge with 4 buttons to navigate) now keep ur this view at top . and all the other hirarchy of views u can add in that data view.

I think you should create a singleton UIViewController or UIView for this. Advantage of the UIViewController is that it can be presented with the presentModalViewController:animated: method, and also if you need, you can push it to your navigation stack. Also an advantage that you have a UIView instance in the same time.
Using the singleton pattern, you can reach your class anywhere.

Check this link from the official documentation about singleton in objective-c: Creating a Singleton Instance
Or you can use Matt Gallagher's macro (because this is very comfortable to use): Singleton macro

If you can get iOS 5 beta, I highly recommend it. The new beta dev release has an extension on UIViewController, dubbed by Apple as container view controllers. They are very easy to set up, and I am using one in my app.

Container view controllers work like UINavigationControllers and UITabBarControllers in that they allow you to add child view controllers, not just child views. Like I've said, they are simple to set up. I'll explain how:

  1. Create a new class called ContainerController. Add an initialization method called - (id)initWithNavigationController:(UINavigationController *)controller. It's implementation should look something like this:

    if ((self = [super init])) {
      [self addChildViewController:controller];
      controller.view.frame = CGRectMake(0, 0, 320, 420); // Leave room for the menu at bottom of the screen
      [self.view addSubview:controller.view];
    }
    

    The reason we are adding a navigation controller is because it keeps this container controller from doing transitions, which is more complicated.

  2. In the ContainerController's viewDidLoad method, create the menu:

    [super viewDidLoad];
    
    UIView *menuWrapperView = [[[UIView alloc] init] autorelease];
    menuWrapperView.frame = CGRectMake(0, 420, 320, 40); // Place a wrapper for the menu buttons directly below the navigation controller
    
    UIButton *button1 = ...;
    UIButton *button2 = ...;
    
    [menuWrapperView addSubview:button1];
    [menuWrapperView addSubview:button2];
    
    [self.view addSubview:menuWrapperView];
    

With this approach (using child view controllers), your views and navigation controller will get their needed memory warning, view loading, and rotation handling method calls when needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top