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

标签 ios user-interface nib tabbar

我有 5 个独立的 TableView Controller nibs (使用自定义单元实现)可通过另一个表格 View 菜单列表访问(无 Storyboard )

客户希望拥有全部 5 个 nibs在选项卡中。所以我需要摆脱菜单列表并提供nibs在选项卡中。

我怎样才能做到这一点 ?

最佳答案

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

@property (strong, nonatomic) UITabBarController *tabBarController;

制作一种方法来设置 View 并设置您的标签栏,例如:
-(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 中调用此方法.

现在设置标签栏的委托(delegate)方法,您可以在那里设置自定义图像:
- (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"];
    }
}

关于ios - 如何使用独立的 NIB 制作标签栏应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21379375/

相关文章:

ios - Xcode View 层次结构调试器 - "prototyping"

iOS 9 - 请求照片的相机授权

python - 我应该嵌入或扩展 python 来创建高质量、高速的 GUI 程序吗?

objective-c - loadNibNamed 方法太慢 - 如何让它更快?

iphone - 确定 iPhone 设备的型号

用于条件绑定(bind)的 iOS Initializer 必须具有 Optional 类型,而不是 'CGFont' ?

java - Swing JTextPane编译错误

java - JTextArea画Java?

ios - 加载空 Nib 导致应用商店评论崩溃

iphone - Interface Builder 中的组 View