Tab Bar disappears below the bottom of the screen

2019-08-05 19:42发布

问题:

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!

回答1:

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