我以编程方式创建TabBarController
,因为我想在显示不同内容的每个选项卡中使用相同的Controller。内容由ID
提取。
我使用storyboard 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个选项卡。不幸的是,“更多”按钮不可见。因此无法访问最后两个选项卡。
有没有人知道如何强制显示更多按钮,或者为什么不显示?
问候!
答案 0 :(得分: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];
答案 1 :(得分:0)
以下是您如何使用UITabBarController
第一次在UIViewController
文件中创建UINavigationController
和AppDelegate.h
的所有对象,并使用以下AppDelegate.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;
}