如何使用独立NIB制作标签栏应用程序?

时间:2014-01-27 11:13:19

标签: ios user-interface nib tabbar

我有5个独立的表视图控制器nibs(带有自定义单元格实现)可通过另一个表视图菜单列表(无故事板)访问

客户希望在标签中包含所有5个nibs。所以我需要摆脱菜单列表并在TAB中提供nibs

我该怎么做?

2 个答案:

答案 0 :(得分:2)

首先将此属性添加到AppDelegate.h

@property (strong, nonatomic) UITabBarController *tabBarController;

制作方法来设置视图并设置标签栏,如:

-(void)setViews
{
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] ;
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
    UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];

    UINavigationController *navigationController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
    [navigationController1.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
    [navigationController2.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
    [navigationController3.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
    [navigationController4.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
    [navigationController5.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];


    [navigationController1.navigationBar setHidden:YES];
    [navigationController2.navigationBar setHidden:YES];
    [navigationController3.navigationBar setHidden:YES];
    [navigationController4.navigationBar setHidden:YES];
    [navigationController5.navigationBar setHidden:YES];


    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController.tabBar setBackgroundColor:[UIColor clearColor]];

    self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar"];
    [[[self tabBarController]tabBar]setSelectionIndicatorImage:[UIImage imageNamed:@"transparent.png"]];
    [self.tabBarController setDelegate:self];
    self.tabBarController.viewControllers = @[navigationController1, navigationController2,navigationController3,navigationController4,navigationController5];
    self.window.rootViewController = self.tabBarController;
}

如果您不想或不想制作自定义导航栏,请避免设置图像和setHidden。 并在didFinishLaunchingWithOptions

中调用此方法

现在设置tabbar的委托方法,您可以在那里设置自定义图像:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (tabBarController.selectedIndex == 0)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-1"];
    }
    else if (tabBarController.selectedIndex == 1)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-2"];
    }
    else if (tabBarController.selectedIndex == 2)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-3"];
    }
    else if (tabBarController.selectedIndex == 3)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-4"];
    }
    else if (tabBarController.selectedIndex == 4)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-5"];
    }
}

答案 1 :(得分:1)

我将它设置在主nib / storyboard中,但在代码中显示更容易。您可以按常规方式创建视图控制器(同样,nib或storyboard或代码)。

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:@[vc1, vc2, vc3, vc4, vc5] animated:YES];
    UIWindow *window = [[UIApplication sharedApplication] delegate].window;
    [window setRootViewController:tabBarController];