views:

237

answers:

1

I added a toolBar with this code:


- (void)viewWillAppear:(BOOL)animated {
    UIBarButtonItem *yesterday = [[UIBarButtonItem alloc]initWithTitle:@"Yesterday" 
                                                                 style:UIBarButtonItemStyleBordered target:self action:@selector(yesterday:)];
    UIBarButtonItem *today = [[UIBarButtonItem alloc]initWithTitle:@"Today" 
                                                             style:UIBarButtonItemStyleDone target:self action:@selector(today:)];
    UIBarButtonItem *tomorrow = [[UIBarButtonItem alloc]initWithTitle:@"Tomorrow" 
                                                                style:UIBarButtonItemStyleBordered target:self action:@selector(tomorrow:)];
    UIBarButtonItem *month = [[UIBarButtonItem alloc]initWithTitle:@"Month" 
                                                             style:UIBarButtonItemStyleBordered target:self action:@selector(month:)];
    NSArray *items = [NSArray arrayWithObjects:yesterday,today,tomorrow,month, nil];

    [yesterday release];
    [today release];
    [tomorrow release];
    [month release];

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    [toolbar sizeToFit];
    [toolbar setFrame:CGRectMake( 0, 20, 320, 40)];
    [toolbar setItems:items];
    [self.navigationController.view addSubview:toolbar];
}

but when i change the view using the navigation controller the toolbar stays there...

how can i remove that subview?

+2  A: 

UINavigationController has a toolbar built in which is hidden by default. You can display it using [navigationController setNavigationBarHidden:animated:];. You might want to use that instead. Then, before you push a view controller, set that view controller's hidesBottomBarWhenPushed property to true.

The reason your toolbar doesn't go away in this instance is that you're adding it to navigationController's view which displays on top of other views it controls. You could instead add it as a subview of self.

To answer your specific question, though, to remove the toolbar from any superview, use [toolbar removeFromSuperview]. In this case, I would go with the cleaner solution of using the toolbar that is built into navigation controllers.

Tim R.
i've tried using the one built in but its displayed at the bottom of the screen and if i move it to the top of the screen it leaves a empty space on the bottom !
Mr_Nizzle
Okay then, you are adding it to the wrong view. You are adding it to navigationController.view, but what you want is the view controlled by the view controller on top of the navigation controller's view controller stack.`[[[navigationController viewControllers] lastObject].view addSubview:toolbar];` (or it might be the first object, I'm not sure)...or, you could probably just use `[self addSubview:toolbar];` in this context.If you add it to that view it should slide off with that view when you push another view controller.
Tim R.
Seems like those options are not working 4 me :S just works if i add the subview with this: [self.navigationController.view addSubview:toolbar];
Mr_Nizzle
y tried this [self.view addSubview:toolbar];and worked out perfect now i just gotta figure out how move the table a couple of pixels so it starts where the toolbar ends XD
Mr_Nizzle