草庐IT

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

coder 2024-01-28 原文

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

案例一:需要显示storyboard中的所有tab items

@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)

}

最佳答案

对于 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:@"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 代码 ...

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

有关iOS:重用 TabBarController 并删除旧 Controller 的一些选项卡的更多相关文章

  1. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  2. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  3. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  4. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  5. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  6. ruby-on-rails - 如何在 Rails Controller Action 上触发 Facebook 像素 - 2

    我有一个ruby​​onrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素

  7. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  8. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  9. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  10. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

随机推荐