【问题标题】:iOS: Reuse the TabBarController with removing some tabs of the old controlleriOS:重用 TabBarController 并删除旧控制器的一些选项卡
【发布时间】:2017-11-13 09:54:36
【问题描述】:

我有一个包含 5 个项目的 TabBarController。我需要在另一个地方重用这个选项卡视图并删除一些项目。我怎样才能做到这一点。只有 isEnabled 按钮用于以编程方式执行此操作。但我需要隐藏标签项。

案例1:需要显示storyboard中的所有标签项

@IBAction func partialAction(_ sender: UIButton) {
    let  partialTabController = storyboard?.instantiateViewController(withIdentifier: "MainTabController") as! MainTabController
    partialTabController.selectedViewController = partialTabController.viewControllers?[3]

    present(partialTabController,animated: true,completion: nil)

}

案例 2:仅显示应用程序另一部分中的几个选项卡

@IBAction func partialAction(_ sender: UIButton) {
    let  partialTabController = storyboard?.instantiateViewController(withIdentifier: "MainTabController") as! MainTabController
    partialTabController.selectedViewController = partialTabController.viewControllers?[3]

    // Can I remove some of the tab item using code here

    present(partialTabController,animated: true,completion: nil)

}

【问题讨论】:

  • 嗨@Krishnakumar CN,如果您添加现有代码的sn-p会有所帮助。

标签: ios swift uitabbarcontroller tabbarcontroller


【解决方案1】:

对于目标 c 通过单个 TabBarController 在不同的屏幕上设置不同的标签栏... 为UITabBarController创建扩展类并创建自定义功能栏

   -(void)customTabBar {
[self.tabBar setBackgroundColor:[UIColor whiteColor]];  //[CommonFunctions colorWithRed:255.0 green:240.0 blue:237.0 alpha:1.0]];
[self.tabBar setBackgroundImage:[[UIImage alloc]init]];
UITabBar *tabBar = self.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];

[item0 setTitle:@"Home"];
[item0 setImage:[[UIImage imageNamed:@"Home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item0 setSelectedImage:[[UIImage imageNamed:@"Homeblue"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[item1 setTitle:@"Browse"];
[item1 setImage:[[UIImage imageNamed:@"Browse"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setSelectedImage:[[UIImage imageNamed:@"Browseblue"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[item2 setTitle:@"Post"];
[item2 setImage:[[UIImage imageNamed:@"Post"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item2 setSelectedImage:[[UIImage imageNamed:@"Postblue"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setTitle:@"Activity"];
[item3 setImage:[[UIImage imageNamed:@"Activity"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setSelectedImage:[[UIImage imageNamed:@"Activityblue"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setTitle:@"Profile"];
[item4 setImage:[[UIImage imageNamed:@"Profile"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setSelectedImage:[[UIImage imageNamed:@"Profileblue"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];


[item0 setImageInsets:UIEdgeInsetsMake(3, 0, -3, 0)];
[item1 setImageInsets:UIEdgeInsetsMake(3, 0, -3, 0)];
[item2 setImageInsets:UIEdgeInsetsMake(3, 0, -3, 0)];
[item3 setImageInsets:UIEdgeInsetsMake(3, 0, -3, 0)];
[item4 setImageInsets:UIEdgeInsetsMake(3, 0, -3, 0)];

}

现在您可以根据自己的需要创建类实例并使用选项卡 ....

并在 appdelegate 中创建你的类的实例,并根据你创建添加选项卡的函数

为扩展类创建实例

 @property (strong, nonatomic) CustomTabBarViewController *tabBarController;


  -(void)createTabBarContoller
{
  //Created all tabs controller to be the default for tabs
  UIViewController *viewController1 = [[HomeViewController alloc] 
  initWithNibName:@"HomeViewController" bundle:nil];
   UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];

  UIViewController *viewController2 = [[SaleViewController alloc] initWithNibName:@"SaleViewController" bundle:nil];
  UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];

  UIViewController *viewController3 = [[SellCameraViewController alloc] initWithNibName:@"SellCameraViewController" bundle:nil];
  UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];

  UIViewController *viewController4 = [[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil];
  UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];

  UIViewController *viewController5 = [[MeViewController alloc] initWithNibName:@"MeViewController" bundle:nil];
  UINavigationController *navController5 = [[UINavigationController alloc] initWithRootViewController:viewController5];

  self.tabBarController = [[CustomTabBarViewController alloc] init];
  self.tabBarController.viewControllers = @[navController1, navController2, navController3, navController4, navController5];

  [self.tabBarController setDelegate:self];
  [self.tabBarController customTabBar];
  self.tabBarController.tabBar.layer.borderWidth = 0.50;
  self.tabBarController.tabBar.layer.borderColor = [UIColor colorWithRed:236.0/255.0 green:236.0/255.0 blue:236.0/255.0 alpha:1].CGColor;
  self.tabBarController.tabBar.clipsToBounds = true;

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

其实我也在用swift,但是现在我没有swift代码...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 2021-11-04
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多