Ios 如何使用UITabBarController打开模式,并在关闭时显示最后一个选项卡栏视图?

Ios 如何使用UITabBarController打开模式,并在关闭时显示最后一个选项卡栏视图?,ios,swift,cocoa-touch,uitabbarcontroller,uistoryboard,Ios,Swift,Cocoa Touch,Uitabbarcontroller,Uistoryboard,所以我在一个有3个按钮的故事板上有一个UITabBarController。其中两个通过标准视图关联序列打开与选项卡栏关联的标准视图。。。第三项是问题 我想在模式视图中打开另一个故事板(由应用程序中的其他视图共享)中的表单,在关闭或提交后,只需返回到选项卡视图中以前处于活动状态的任何选项卡 我已经看到了一些“选项”,但没有一个看起来和我想做的完全一样 有什么想法吗 编辑:它被一个mod从我的原始问题中编辑出来,但我是用Swift写的…不是Obj-C。我模拟了相同的,请参考链接:- 运行项目 轻触

所以我在一个有3个按钮的故事板上有一个
UITabBarController
。其中两个通过标准视图关联序列打开与选项卡栏关联的标准视图。。。第三项是问题

我想在模式视图中打开另一个故事板(由应用程序中的其他视图共享)中的表单,在关闭或提交后,只需返回到选项卡视图中以前处于活动状态的任何选项卡

我已经看到了一些“选项”,但没有一个看起来和我想做的完全一样

有什么想法吗


编辑:它被一个mod从我的原始问题中编辑出来,但我是用Swift写的…不是Obj-C。

我模拟了相同的,请参考链接:-

  • 运行项目
  • 轻触Tab2
  • 您将看到3个按钮
  • 轻敲按钮3

  • 听起来你并不真的想要一个标签,标签应该包含一个视图,而不是一个新的/模式视图。然而,在某些情况下,你想做的事情是有意义的,比如一些应用程序将使用“中心”选项卡作为按钮来添加新项目或执行某些操作(Yelp就是一个很好的例子)。在这些情况下,选项卡通常较大或视觉上不同,表示它不是真正的选项卡。这就是我认为你真正想要的

    为此,您需要:

  • 选项卡栏
    中创建一个选项卡,这只是一个占位符
  • 创建一个图标或图像,用作将在占位符选项卡上显示的按钮
  • 使用
    tabBarController
    类的
    viewWillLayoutSubviews
    将按钮放置在占位符选项卡上。请参见下文(纵向示例代码)
  • 将动作添加到显示模态视图的按钮
  • 注意:您可能需要使用下面的代码调整按钮的位置,以符合您的需要

    -(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
                if (UIDeviceOrientationIsPortrait && !centerTabButton) {
    
                    //CUSTOM CENTER TAB BAR BUTTON
                    UIImage *buttonImage = [UIImage imageNamed:@"icn_CenterTabBarIcon.png"];
                    UIImage *highlightImage = [UIImage imageNamed:@"icn_CenterTabBarIcon.png"];
    
                    centerTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    centerTabButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
                    [centerTabButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
                    [centerTabButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
                    [centerTabButton setShowsTouchWhenHighlighted:FALSE];
                    [centerTabButton setAdjustsImageWhenHighlighted:FALSE];
                    [centerTabButton addTarget:self action:@selector(centerTabClicked:) forControlEvents:UIControlEventTouchUpInside];
    
                    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    
                    //center the button on the tab bar based on the tab bar's height
                    if (heightDifference < 0){
                        centerTabButton.center = self.tabBar.center;
                    }
                    else{
                        CGPoint center = self.tabBar.center;
                        center.y = center.y - heightDifference/2.0;
                        centerTabButton.center = center;
                    }
    
                    [self.view addSubview:centerTabButton];
                }
            }
    
    -(void)视图将布局子视图{
    [超级视图将布局子视图];
    if(UIDeviceOrientationSportRait&&!中心选项卡按钮){
    //自定义中心选项卡栏按钮
    UIImage*buttonImage=[UIImage ImageName:@“icn_CenterTabBarIcon.png”];
    UIImage*highlightImage=[UIImage ImageName:@“icn_CenterTabBarIcon.png”];
    centerTabButton=[UIButton按钮类型:UIButtonTypeCustom];
    centerTabButton.frame=CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
    [centerTabButton setBackgroundImage:buttonImage for状态:UIControlStateNormal];
    [centerTabButton setBackgroundImage:highlightImage for状态:uicontrol状态高亮显示];
    [CenterTab按钮设置当高亮显示时显示为:FALSE];
    [中心选项卡按钮设置调整高亮度时的同步:假];
    [centerTabButton添加目标:自我操作:@选择器(centerTabClicked:)用于控制事件:UIControlEventTouchUpInside];
    CGFloat heightDifference=buttonImage.size.height-self.tabBar.frame.size.height;
    //根据选项卡栏的高度将按钮居中放置在选项卡栏上
    如果(高差<0){
    centerTabButton.center=self.tabBar.center;
    }
    否则{
    CGPoint center=self.tabBar.center;
    center.y=center.y-高度差/2.0;
    centerTabButton.center=中心;
    }
    [self.view addSubview:centerTabButton];
    }
    }
    
    来回答您关于它“易碎”的第二个问题


    如果为构成
    tabBarController
    选项卡的所有根viewController提供标题,则可以使用viewController.title检查在
    shouldSelectViewController
    委托中点击的选项卡。当然,您仍然会面临有人重命名视图标题属性的风险。

    因此,我有一个潜在的解决方案,可以使用空白视图控制器,应该选择ViewController和UITabBarController委托。唯一的问题是,我对它的脆弱性不是特别满意

    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    
        // This is a bit dangerous if we move where 'newController' is located in the tabs, this will break.
        let newController = viewControllers![1] as! UIViewController
    
        // Check if the view about to load is the second tab and if it is, load the modal form instead.
        if viewController == newController {
            let storyboard = UIStoryboard(name: "ModalController", bundle: nil)
            let vc = storyboard.instantiateInitialViewController() as? UIViewController
    
            presentViewController(vc!, animated: true, completion: nil)
    
            return false
        } else {
            return true
        }
    }
    
    这很好,但是如果有人来重新安排故事板中选项卡的顺序,这将中断


    为了给@rmp一些信任,另一个选项是“如果为构成tabBarController中选项卡的所有根viewController提供标题,则可以使用viewController.title检查在shouldSelectViewController委托中点击的选项卡。当然,您仍然会面临有人重命名视图标题属性的风险。“

    正如帖子中的标签所示,我使用的是Swift,不是Obj-C……但我明白你的意思。我希望能够保持在故事板上的东西与自动布局和建设的东西,以及编程,只是由于前进的道路,由苹果公司制定。我想知道我是否可以使用UITabBarController委托拦截选项卡点击。是的,这段代码只是现有live应用程序的副本,翻译成swift应该很简单。是的,您可以覆盖
    tabBarController:shouldSelectViewController:
    ,然后使用其他自定义代码来显示模式,但您仍然需要在故事板之外做一些工作。请参阅上面我更新的代码…它可以工作,但我对它的脆弱性有点不满意…有什么想法吗?@rmp如何在tabBar中创建占位符选项卡?如果我创建tabBar项,那么我必须为此分配一个Viewcontroller,对吗?对…我想这样的东西不能完全防止。而且…你真的不必为每个根视图控制器都有一个标题…只是你正在检查的那个。@kevindeleon true,我只是想保持一致;)避免移动选项卡崩溃的更好方法是:让newController={viewControllers!.filter{$0是MyTabViewControllerSubclass}.first!}()