Search code examples
iosuinavigationbaruitabbar

Toolbar + Navigation Bar + Segmented Control?


I'm trying to find a way to layout an application that includes a tab bar on the bottom, a navigation bar on the top, and a row of buttons on the navigation bar that switches views (on the first tab).

I've drawn a very rough sketch (sorry!), but I hope it illustrates the intent.

App layout On the bottom, there are two tabs (tab1, and tab2).

When Tab1 is selected, the navigation bar will have 3 buttons that will show different views (tab1_1, tab1_2, tab1_3).

When Tab2 is selected, the navigation bar won't show any buttons, but rather some simple text.

At this point, I have the following scheme in my application delegate's didFinishLaunchingWithOptions:

    UIViewController *viewController1 = [[Tab1_ViewController alloc] initWithNibName:@"Tab1_ViewController" bundle:nil];
    UIViewController *viewController2 = [[Tab2_ViewController alloc] initWithNibName:@"Tab2_ViewController" bundle:nil];

    tab1NavController = [[UINavigationController alloc] initWithRootViewController:viewController1];
    tab2NavController = [[UINavigationController alloc] initWithRootViewController:viewController2];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:tab1NavController, tab2NavController, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

I was wondering if I need to redo how I'm doing things in order to achieve the layout as in the picture.

Any help would be appreciated, thank you!


Solution

  • I have done this for my current project...i hope this will help you....

    At first take UITabbarController at your first viewController [first sketch you have given]

    For your first view use this code....

     - (void)viewDidLoad {
    [super viewDidLoad];
    
    dashBoardView = [[DashboardViewController alloc] initWithNibName:@"DashboardViewController" bundle:nil];
    dashBoardView.title = @"dashBoardView";
    UINavigationController *mydashboarController = [[[UINavigationController alloc] initWithRootViewController:dashBoardView] autorelease];
    mydashboarController.navigationBar.barStyle = UIBarStyleBlack;
    [listOfViewControllers addObject:mydashboarController];
    [dashBoardView release];
    
    ordersView = [[OrdersViewController alloc] initWithNibName:@"OrdersViewController" bundle:nil];
        ordersView.title = @"ordersView";
    UINavigationController *myorderController = [[[UINavigationController alloc] initWithRootViewController:ordersView] autorelease];
    myorderController.navigationBar.barStyle = UIBarStyleBlack;
    [listOfViewControllers addObject:myorderController];
    [ordersView release];
    
    orderList = [[OrderListViewController alloc] initWithNibName:@"OrderListViewController" bundle:nil];
    orderList.title = @"orderList";
    UINavigationController *myorderListController = [[[UINavigationController alloc] initWithRootViewController:orderList] autorelease];
    myorderListController.navigationBar.barStyle = UIBarStyleBlack;
    [listOfViewControllers addObject:myorderListController];
    [orderList release];
    
    productView = [[ProductViewController alloc] initWithNibName:@"ProductViewController" bundle:nil];
        productView.title = @"productView";
    UINavigationController *myproductController = [[[UINavigationController alloc] initWithRootViewController:productView] autorelease];
    [listOfViewControllers addObject:myproductController];
    [productView release];
    
    [self.tabBarController setViewControllers:listOfViewControllers animated:YES];
    
    NSArray *segmentTextContent = [NSArray arrayWithObjects:NSLocalizedString(@"Dashboard", @""),NSLocalizedString(@"Order", @""),
                                   NSLocalizedString(@"Product", @""),NSLocalizedString(@"More", @""),
                                   nil];
    UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.frame = CGRectMake(0, 0, 400, 40);
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    //defaultTintColor = [segmentedControl.tintColor retain];   // keep track of this for later
    
    segmentedControl.tintColor = [UIColor colorWithHue:8.0 saturation:8.0 brightness:8.0 alpha:1.0];
    segmentedControl.alpha = 0.8;
    
    self.navigationItem.titleView = segmentedControl;
    [segmentedControl release]; 
    }
    

    If it is not clear to you then please knock...