• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS:重用 TabBarController 并删除旧 Controller 的一些选项卡

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:17:05 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个包含 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap