views:

224

answers:

3

In the iPhone maps app there's a toolbar at the bottom of the map view (it contains the Search/Directions segment control and others). When moving from the map view by clicking on a callout, the toolbar slides out with the map view, leaving the next view (a table controller) with no toolbar.

I've tried to do the same thing with [self.navigationController setToolbarHidden:YES animated:YES] in the second view controller, but this gives a strange toolbar sliding down animation, while the map view is sliding to the left.

Using [self.navigationController setToolbarHidden:YES] in viewDidLoad also causes a bad effect (it makes the toolbar disappear the moment the push animation starts, leaving an ugly white space).

I'm assuming the answer to this is to use a nib file, but I'd prefer to do it programatically (if possible).

How can I get the toolbar to "stick" to the map view and slide out with it when I push a new view controller? Thanks.

Gourmet Haus Staudt

A: 

Override the second view controller's -viewWillAppear: method to hide the toolbar.

Alex Reynolds
Thanks for the answer. I had a mistake above, I was using `-viewDidLoad`, not `-viewDidLoad:animated` (which doesn't exist). Hiding the toolbar in `-viewWillAppear:animated` has the same effect, it either hides the toolbar immediately, or does the slide out animation while pushing the view controller.
nevan
+1  A: 

Hi! I was around this for a day once. Really dont get the programatically answer, but the best way to views to behave correctly, is to do the interface in the interface builder. If you set items for a toolbar in your code like: [self.navigationController setToolbarItems: control1, control2,..., nil] animated: NO];

with my little experience, I can say that you are saying to the entire application to have a toolbar present when you push new views unless you hide it (or you are using a tabBar), but hiding it you get those unwanted effects.

You can try this:

[self.navigationController setToolbarHidden:YES animated:YES];

in your first controller - (void)viewWillDisappear:(BOOL)animated method, and setting hidden to NO in - (void)viewWillAppear:(BOOL)animated method in the first controller too.

Hope this helps.

PS: And if you get the programatically answer, let me know! =P

Omer
Hi Omer.I don't fully understand view controllers, so I like to do everything in code to learn. What you said about the whole app having the toolbar is right, since it's attached to the navigation controller. Your answer gave me the clue I needed. I created the toolbar myself and added it as a subview. Thanks for the help. I posted the code as an answer.
nevan
+1  A: 

It turns out the answer is to create the toolbar directly and add it to the view yourself. This is in the code for a UIViewController with a UINavigationController. The frame coordinates can change according to what is on screen.

- (void)viewDidLoad
{
    // Add a toolbar to the view
    CGRect toolbarFrame = CGRectMake(0, 372, 320, 44);
    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];

    UIBarButtonItem *compassButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"compass.png"]
                                                                      style:UIBarButtonItemStyleBordered
                                                                     target:self
                                                                     action:@selector(zoomToCurrentLocation)];

    compassButton.width = 30.0f; // make the button a square shape
    [myToolbar setItems:[NSArray arrayWithObject:compassButton] animated:NO];
    [compassButton release];

    [self.view addSubview:myToolbar];
    [super viewDidLoad];
}
nevan