views:

286

answers:

1

Hi,

In my application I'm using a Navigation Controller to push one view that loads a Tab Bar Controller and a custom Navigation Bar as well. The problem is that the Tab Bar disappears below the bottom of the screen, and I don't know what's causing the problem.

If I load a simple Tab Bar in the next view, it positions itself correctly... but what I need is a Tab Bar Controller, and in that case the Tab Bar disappears below the bottom. I have tried changing the view and size properties of the Tab Bar, but that did not solve the problem.

I also realised that the images and text of the tabs don't show (I have set up the "favourites" and "contacts" images and text, and they are big enough and should be visible on the top side of the tab, but they are not).

Both tabs work perfectly, by the way.

There is an image here.

I load the Tab Bar with the following code:

- (void)viewDidLoad {
    [super viewDidLoad];
    myTabBarController = [[UITabBarController alloc] init];
    SettingsViewController* tab1 = [[SettingsViewController alloc] init];   
    AboutViewController* tab2 = [[AboutViewController alloc] init];
    NSArray* controllers = [NSArray arrayWithObjects:tab1, tab2, nil];
    myTabBarController.viewControllers = controllers;
    [self.view insertSubview:myTabBarController.view belowSubview:myNavigationBar];
}

It doesn't matter if I remove the Navigation Bar or not. I have tested using this instead:

[self.view addSubview:myTabBarController.view];

... forgetting about the Navigation Bar, but the Tab Bar still goes under the bottom.

I don't know if the problem is in one of my NIB files or in how I load the view (although I do this as I read in the Apple's SDK documentation). Any ideas?

Another question would be... do you know how could I change the title of my Navigation Bar when I select the second tab? I imagine I would have to do it in viewDidLoad in AboutViewController.m, would that be correct?

Thanks for you time!

A: 

Please ask one question per submission - it will allow us to better help you.

For your first problem: you need to add the tab bar controller to the navigation controller, not to that view. The heirarchy should be:

  • Navigation Controller
    • Tab Bar Controller
      • Your View(s)

(Though Apple's documentation suggests that the tab bar should be application-wide and should thus be the root view, not the navigation controller).

For your second question, the text on the "Back" button is determined by the title property of the previous view controller. To change it, do the following (source):

- ( void )viewDidLoad
{
    [ super viewDidLoad ];

    UIBarButtonItem *backButton =
    [[ UIBarButtonItem alloc ] initWithTitle:@"Back"
                                       style:UIBarButtonItemStyleBordered
                                      target:nil
                                      action:nil ];
    self.navigationItem.backBarButtonItem = backButton;
    [ backButton release ];
}

EDIT: It appears you want to set the title of the second view controller in your navigation stack. That's easy:

- ( void )viewDidLoad
{
    [ super viewDidLoad ];

    self.title = @"Title";
}
Jeff Kelley
Hi Jeff, thanks for the answer! I created a structure like that because the view where I have the Tab Bar Controller is being pushed from the root view of the Navigation Controller... so I push a view (the second view in the navigation stack) and then add the Tab Bar Controller to it. Is that incorrect? I could not find any other way to push the second view (where I have the Tab Bar Controller). Everything works fine with that settings, the only problem is the Tab Bar going below the bottom of the screen.
Manu
Sorry about asking two questions at the same time, won't happen again. Anyway, in my second question what I want to change is the title of my Navigation Bar when I switch to the second tab (that Navigation Bar is NOT the Navigation Controller's Navigation Bar, but another Navigation Bar that I created separately and that is superimposed in the view (so changing the title of the view is not enough... I probably would have to change the title of the bar itself, but the bar is declared on the previous view).
Manu
For the tab bar: Can you try using its `frame` property to position it? You should be able to place it wherever you like.For the navigation bar: You'll have to store a pointer to it somewhere - maybe define an ivar in your second view controller.This is all very non-standard stuff. I don't know what your final goal is, but you may find more useful things in the View Controller Programming Guide: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Jeff Kelley
Thanks again Jeff. I think that what I'm trying to do is actually not supported, I have fixed my problem loading a single Tab Bar instead a Tab Bar Controller (according to Apple, this one cannot be pushed through a View inside a Navigation Controller). I explain it here: http://stackoverflow.com/questions/2781056/view-loading-problem/2782606#2782606
Manu