1

我以TabBarController编程方式创建我的,因为我希望每个选项卡中的相同控制器显示不同的内容。内容由ID. 我使用故事板 id 的方式与使用initWithNibName:.

我在 AppDelegate 中执行此操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    for (int i = 0; i < 7; i++) {
        MyViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"MyView"];
        [svc setID: i];
        [tabBarController addChildViewController:svc];
    }

    [self.window makeKeyAndVisible];
    [self.window setRootViewController: tabBarController];

    return YES;
}

但 TabBar 仅显示 7 个选项卡中的 5 个。这很好,因为同时只能看到 5 个选项卡。不幸的是...更多按钮不可见。所以最后 2 个选项卡无法访问。

有谁知道如何强制显示更多按钮,或者为什么它不显示?

问候!

4

2 回答 2

1

它没有显示,因为您正在使用方法将视图控制器添加到标签栏控制器addChildViewController,该方法是一种UIViewController方法,而不是标签栏控制器方法。所以我认为正在发生的事情是标签栏控制器并不真正知道它有超过 5 个视图控制器。

如果要显示更多的视图控制器,请直接设置标签栏控制器的 viewControllers 数组。将您的代码修改为如下所示:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];  
NSMutableArray *vcArray = [NSMutableArray array]; 
for (int i = 0; i < 7; i++) {
    ViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"MyView"];
    [svc setID: i];
    [vcArray addObject:svc];
}
[tabBarController setViewControllers:vcArray]; //This is the important part.
[self.window setRootViewController: tabBarController];
[self.window makeKeyAndVisible];
于 2013-03-13T11:26:21.547 回答
0

以下是如何使用的简单示例UITabBarController

首先创建文件中的所有对象UIViewController并使用以下方法UINavigationControllerAppDelegate.hAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds ]];

    self.viewCon=[[ViewController alloc] init];
    self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
    self.navCon.navigationBar.tintColor=[UIColor blackColor];
    self.viewCon.title=@"First View";

    self.fView=[[FirstViewController alloc] init];
    self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
    self.FnavCon.navigationBar.tintColor=[UIColor blackColor];

    self.fView.title=@"Secound View";

    self.sView=[[SecoundViewController alloc] init];
    self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
    self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
    self.sView.title=@"Third View";
    .
    .
    // create UIViewController and UINavigationController As you need 
    .
    .
    .
    UIImage *img1=[UIImage imageNamed:@"Australia.gif"];
    self.tbItem1=[[UITabBarItem alloc] initWithTitle:@"First Page" image:img1 tag:1];
    self.viewCon.tabBarItem=self.tbItem1;

    UIImage *img2=[UIImage imageNamed:@"Cameroon.gif"];
    self.tbItem2=[[UITabBarItem alloc] initWithTitle:@"Secound Page" image:img2 tag:2];
    self.fView.tabBarItem=self.tbItem2;

    UIImage *img3=[UIImage imageNamed:@"Canada.png"];
    self.tbItem3=[[UITabBarItem alloc] initWithTitle:@"Third Page" image:img3 tag:3];
    self.sView.tabBarItem=self.tbItem3;

    NSMutableArray *viewArr=[[NSMutableArray alloc] init];
    [viewArr addObject:self.navCon];
    [viewArr addObject:self.FnavCon];
    [viewArr addObject:self.SnavCon];


    self.tbCon=[[UITabBarController alloc] init];
    self.tbCon.viewControllers=viewArr;

    [self.window addSubview:tbCon.view];

    [self.window makeKeyAndVisible];

    return YES;
}
于 2013-03-13T10:59:51.000 回答