我必须通过另一个表视图菜单列表中访问5独立表视图控制器nibs
(通过自定义单元实现)(无故事板)如何使标签栏应用程序脱离独立的NIB?
客户希望在所有选项卡5 nibs
。所以我需要摆脱菜单列表并在TAB中提供nibs
。
我该怎么做?
我必须通过另一个表视图菜单列表中访问5独立表视图控制器nibs
(通过自定义单元实现)(无故事板)如何使标签栏应用程序脱离独立的NIB?
客户希望在所有选项卡5 nibs
。所以我需要摆脱菜单列表并在TAB中提供nibs
。
我该怎么做?
首先这个属性添加到您的AppDelegate.h
@property (strong, nonatomic) UITabBarController *tabBarController;
使设置视图的方法和设置您的TabBar一样:
-(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"];
}
}
我会将它放在主要的笔尖/故事板中,但它更容易在代码中显示。您可以用通常的方式创建视图控制器(再次,笔尖或故事板或代码)。
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:@[vc1, vc2, vc3, vc4, vc5] animated:YES];
UIWindow *window = [[UIApplication sharedApplication] delegate].window;
[window setRootViewController:tabBarController];