如何使用 UITabBarController 打开模式并在关闭时显示最后一个标签栏视图?

How to open a modal with UITabBarController and on close, display last tab bar view?

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

我想在 modal 视图中打开另一个情节提要中的表单(由应用程序中的其他视图共享),并在关闭或提交后,只需 return 到任何选项卡以前在选项卡式视图中处于活动状态。

我已经看到一些 "options",但其中 none 看起来正是我想要做的。

有什么想法吗?

编辑:它被 mod 从我原来的问题中编辑出来,但我是在 Swift 中写的...不是 Obj-C。

我也模拟过,参考link :-https://github.com/deepesh259nitk/MultipleStoryBoards/tree/master

  1. 运行 项目
  2. 点击 Tab2
  3. 您将看到 3 个按钮
  4. 点击按钮 3

听起来您并不是真的想要选项卡,选项卡旨在包含视图而不是呈现 new/modal 视图。但是,在某些情况下,您想要做的事情是有意义的,例如某些应用程序会将中心选项卡用作添加新项目或执行某些操作的按钮(Yelp 就是一个很好的例子)。在这些情况下,选项卡通常更大或视觉上不同,以表示它不是真正的选项卡。这就是我认为你真正想要的。

为此您需要:

  1. tabBar中创建一个选项卡,这只是一个占位符
  2. 创建一个图标或图像以用作将覆盖占位符选项卡的按钮
  3. 使用 tabBarController class 的 viewWillLayoutSubviews 将按钮放置在占位符选项卡上。见下文(纵向示例代码)。
  4. 向将显示模态视图的按钮添加一个动作。

注意:您可能需要使用以下代码调整按钮的位置以满足您的需要。

-(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];
            }
        }

回答你的第二个问题"brittle"

如果您为构成 tabBarController 中选项卡的所有根视图控制器提供标题,那么您可以使用 viewController.title 检查 [=11= 中哪个选项卡被点击了] 代表。当然,您仍然 运行 有人重命名您的观点标题的风险 属性。

所以我有一个潜在的解决方案,可以使用空白视图控制器、shouldSelectViewController 和 UITabBarController Delegate。唯一的问题是,我对它的脆弱程度不是特别满意。

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一些功劳,另一个选择是"If you provide a title for all of your root viewControllers that make up the tabs in your tabBarController than you you can use viewController.title to check which tab was tapped in the shouldSelectViewController delegate. Of course you still run the risk of someone renaming your views title property."