Jamie McAllister
Jamie McAllister

Reputation: 729

use storyboard with custom UITabbarController

i have been working on this issue for some time now and cannot find a solution to my problem.

i have a tabbar view controller that i have tried to customise with images, i have the custom graphics working however i need to use code to display and init the tabbar's view controllers. i also have a problem with displaying a navigation bar at the top of one of my tabs which i think is connected to how i am initiating the tab view controllers

the storyboard shows that there should be a navigation bar at the top of the medication tab and that the view is connected to the tab bar via a segue you can see i have tried to use storyboard segues to link my view controllers to the tab bar controller. i have the following code in the MedicationViewController.m

/
//  MedicationViewController.m
//  fibromapp
//
//  Created by jamie mcallister on 08/09/2013.
//  Copyright (c) 2013 Jamie McAllister. All rights reserved.
//

#import "MedicationViewController.h"
#import "TakenViewController.h"
#import "MedsListViewController.h"
#import "MedsAlarmViewController.h"

@interface MedicationViewController ()

@end

@implementation MedicationViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        TakenViewController *viewController2 = [[TakenViewController alloc] init];
        MedsListViewController *viewController1 = [[MedsListViewController alloc] init];
        MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init];


        self.viewControllers = [NSArray arrayWithObjects:viewController1,
                                viewController2,
                                viewController3,nil];
       UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Medication" image:[UIImage imageNamed:NULL] tag:1];
        UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Taken" image:[UIImage imageNamed:NULL] tag:2];
        UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:@"Alarms" image:[UIImage imageNamed:NULL] tag:3];
        UIImage* sel = [UIImage imageNamed:@"fmtabSel"];


        [viewController1 setTabBarItem:tab1];
        [viewController2 setTabBarItem:tab2];
        [viewController3 setTabBarItem:tab3];

        UIImage* tabBarBackground = [UIImage imageNamed:@"fmtab.png"];

        UITabBar *tabBar = self.tabBar;
        [tabBar setBackgroundImage:tabBarBackground];
        [tabBar setSelectionIndicatorImage:sel];
    }
    return self;
}


- (void)viewDidLoad
{
UITabBar *tabbar = self.tabBar;
NSLog(@"%f %f", tabbar.frame.size.width, tabbar.frame.size.height);//used to find the size of the bar
[super viewDidLoad];

UIImage* tabBarBackground = [UIImage imageNamed:@"fmtab.png"];
UIImage* sel = [UIImage imageNamed:@"fmtabSel"];
    UITabBar *tabBar = self.tabBar;
    [tabBar setBackgroundImage:tabBarBackground];
    [tabBar setSelectionIndicatorImage:sel];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

with this code i get the tab but there is no navigation bar at the top of this tab.

can anybody suggest what i must do to resolve this?

if you require any more information feel free to ask and i will edit it into the bottom of this question. thanks in advance :)

Upvotes: 0

Views: 2076

Answers (2)

Leonardo
Leonardo

Reputation: 9857

To have a Navigation bar, you have to put a UINavigationController between the tabbar controller and the first UIViewController.

All can be done in storyboard without needs of writing a line of code.

Upvotes: 1

Stas
Stas

Reputation: 9935

If you want the navigation bar to be at the top You should fill your tabbar controller with navigation controllers inited with root controllers, not just plain controllers. Smth like that:

TakenViewController *viewController2 = [[TakenViewController alloc] init];
MedsListViewController *viewController1 = [[MedsListViewController alloc] init];
MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init];
UINavigationController * nc1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController * nc2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController * nc3 = [[UINavigationController alloc] initWithRootViewController:viewController3];

self.viewControllers = [NSArray arrayWithObjects:nc1,
                                nc2,
                                nc3,nil];

Upvotes: 0

Related Questions