开发者

Remove a toolbar when pushing a new view

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 c开发者_JAVA技巧ontroller) 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 http://img.skitch.com/20100518-xfubyriig48d3ckaemjg2ay8q.jpg


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];
}


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜