OStack程序员社区-中国程序员成长平台

标题: iOS:重用 TabBarController 并删除旧 Controller 的一些选项卡 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:17
标题: iOS:重用 TabBarController 并删除旧 Controller 的一些选项卡

我有一个包含 5 个项目的 TabBarController。我需要在另一个地方重用这个选项卡 View 并删除一些项目。我怎样才能做到这一点。只有 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)

}



Best Answer-推荐答案


对于 objective-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"ost"];
[item2 setImage:[[UIImage imageNamed"ost"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item2 setSelectedImage:[[UIImage imageNamed"ostblue"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setTitle"Activity"];
[item3 setImage:[[UIImage imageNamed:@"Activity"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setSelectedImage:[[UIImage imageNamed:@"Activityblue"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setTitle:@"rofile"];
[item4 setImage:[[UIImage imageNamed:@"rofile"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setSelectedImage:[[UIImage imageNamed:@"rofileblue"] 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代码...

关于iOS:重用 TabBarController 并删除旧 Controller 的一些选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47261475/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4