Search code examples
iphoneobjective-cuitabbarcontrolleruitabbar

UITabBarController only showing half of its UITabBar (off screen)


My UITabBar is not completely showing after I present a UITabBarController from a UIViewController. Please can you tell me what I am doing wrong?

My code is:

//some method

LoggedInViewController *lvc = [[[LoggedInViewController alloc] initWithAccount:account] autorelease];
[self presentModalViewController:lvc animated:YES];

- (void)viewDidLoad

{
    self.tabController = [[UITabBarController alloc] init];
    LoggedInFeedNavigationController *navController = [[LoggedInFeedNavigationController alloc] initWithAccount:self.account];
    [self.tabController setViewControllers:[NSArray arrayWithObject:navController]];
    [self.view addSubview:self.tabController.view];
    [super viewDidLoad];
}

Solution

  • It's not a good practice to do:

    [viewController1.view addSubview:viewController2.view];
    

    The point of the MVC design is lost. The view controller should get your data (from the model) and put it in the view. If you have more than one view just arrange the functionality of the views to accept the corresponding data.

    So if you need a tab bar controller you should do the following:

    // assuming you are in the same initial controller
    UITabBarController* pTabBarControllerL = [[UITabBarController alloc] init];
    MyFirstController* pFirstControllerL = [[MyFirstController alloc] init];
    [pTabBarControllerL setViewControllers:[NSArray arrayWithObject:pFirstControllerL]];
    // perhaps set more tab bar controller properties - button images and so on
    [self presentModalViewController:pTabBarControllerL animated:YES];
    // release the memory you do not need
    
    -(void)viewDidLoad {
        // do your work in pFirstControllerL
    }
    

    PS: You should not subclass UINavigationController and UITabBarController.