工具栏+导航栏+分段控制?

时间:2011-11-22 02:31:24

标签: ios uinavigationbar uitabbar

我正在尝试找到一种方法来布局应用程序,其中包括底部的标签栏,顶部的导航栏以及导航栏上用于切换视图的按钮行(在第一个选项卡上)。

我画了一张非常粗略的草图(抱歉!),但我希望它能说明这一点。

App layout 在底部,有两个选项卡(tab1和tab2)。

选择Tab1时,导航栏将有3个按钮,可显示不同的视图(tab1_1,tab1_2,tab1_3)。

选择Tab2时,导航栏不会显示任何按钮,而是显示一些简单文本。

此时,我在我的应用程序委托的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];

我想知道我是否需要重做我正在做的事情,以实现图片中的布局。

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:3)

我为我当前的项目做了这个...我希望这会对你有帮助....

首先在第一个viewController [你给出的第一个草图]

上取UITabbarController

对于您的第一个视图,请使用此代码....

 - (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]; 
}

如果你不清楚那么请敲......