0
votes

I have a UIViewController called DashBoardViewController that acts as delegate for a UITabBar. In its xib I have placed a UITabBar with 3 UITabBarItem.

Each of these items activate a different View Controller, let's call them ViewController1, ViewController2, ViewController3

DashBoardViewController is supposed to show ViewController1 and select the first bar on loading, so in my initWithNibName I have what follows:

...
ViewController1* vc = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
[self.view addSubview:vc.view]; 
self.currentViewController = vc;
...

I implement the UITabBarDelegate having something as follows:

if (item == viewController1Item) {
    ViewController2 *vc2 = [self.childrenControllers objectAtIndex:1];


    [self.currentViewController.view removeFromSuperview];
    [self.view addSubview:vc2.view];
    self.currentViewController = vc2;


} ...

Problem The View Controller in the first UITabBarItem always works as expected, extending it to the full size of thew view. However, in the second and following tabs, this doesn't happen: the view doesn't extends. This shows if, for example, I align a tab with the bottom in the ViewController2 XIB: this will not be at the bottom when viewed inside the UITabBarItem.

Note Please note that this is not related to the XIB: if I invert ViewController1 and ViewController2, it will be ViewController1 the one failing to extend. It's related to the UITabBarItem.

Ideas Possibly, this depends by the way I addSubview when I call the DashBoardViewController's initWithNibName. But I can't find a way to explain this.

Other details All the XIB are set with "Size = none".

2

2 Answers

1
votes

I can't really speak to the way you have your XIB setup without seeing it, but I can make a couple of suggestions.

The behaviour that you're trying to implement by removing & adding subviews to DashBoardViewController should really be handled by a UITabBarController. This provides a UITabBar, a view for your content and handles the logic of switching between UIViewControllers while keeping layout sane and being part of the SDK.

If for some reason you can't, or don't want to use a UITabBarController, I'd suggest implementing a viewWillLayoutSubviews method on your DashBoardViewController, like so:

- (void)viewWillLayoutSubviews
{
    if( self.currentViewController )
    {
        self.currentViewController.view.frame = self.view.bounds;
    }
}

Maybe also try adding the self.currentViewController.view.frame = self.view.bounds; line after you've swapped ViewControllers too, for good measure. This will make sure that the frame of your current ViewController's view is always sized to fill the bounds of DashBoardViewController's view.

This isn't the 'Proper' way to do it though, I'd really recommend using a UITabBarController if you can, since you don't know how much else of UITabBarController you'll end up re-implementing if you start rolling your own controller.

Any further problems will most probably be to do with the internal layout of your sub-ViewControllers, rather than their size / position in DashBoardViewController's view.

0
votes

On your XIB File make sure that your set the flexible height to stick to top and bottom, this way the UITableView will always have the same height as the 4" display

enter image description here