iOS如何在应用程序的第二个屏幕中添加标签栏



我想制作第一个是登录屏幕的应用程序,我不希望其中的选项卡栏,因为我想要第二个屏幕的选项卡栏。但是当我在应用程序代表中编写选项卡栏代码时,它也会出现在登录屏幕中。那么如何编码呢?

把它放在要添加标签栏的控制器屏幕中

UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = @"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = @"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = @[vc1,vc2];
tabBar.selectedIndex   = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];

如果您使用的是故事板,那么我建议使用以下方法:

拖动UIViewController并将其嵌入UINavigationController周围。这将是您的登录视图控制器,包含用户名,密码和登录按钮。

现在将UITabbarController添加到情节提要上,并将其Storyboard Id为"mainTab",用于保存选项卡项。

现在,只需从LoginViewController连接登录按钮,并在要登录到选项卡栏控制器时添加以下代码行。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:@"MainTab"];
self.navigationController.navigationBarHidden=YES;
[self.navigationController pushViewController:obj animated:YES];

的,它假设您的故事板的名称是主故事板。希望对您有所帮助。

从 appDelegate 显示您的登录屏幕。然后从登录视图控制器显示第二个视图控制器与选项卡栏。使用以下代码。

//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];
//FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need)
FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];
//navigating to the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];

最新更新