从标签栏控制器以模态方式呈现视图

Jos*_*rer 6 popup uitabbarcontroller ios swift

如何从标签栏控制器以模态方式呈现视图,以便视图覆盖实际视图?

我想用相机构建一个视图.像"WhatsApp"或"Ins​​tagram"这样的东西,中间有一个按钮,用户可以点击并显示摄像机视图.

此外,用户应该在单击关闭按钮之前移动他之前的选项卡.

这就是我的ViewController连接到TabBarController的方式: 在此输入图像描述

WsC*_*ndy 11

我必须在我正在构建的应用程序中实现类似的东西,这是相对简单的,你需要实现一个委托方法UITabBarController来实现这一点.

您需要实现的委托方法是: tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool

从此方法返回false将阻止选项卡控制器选择选项卡,然后您只需要实现自己的逻辑以UIViewController编程方式呈现.

这是一个例子:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

    // If your view controller is emedded in a UINavigationController you will need to check if it's a UINavigationController and check that the root view controller is your desired controller (or subclass the navigation controller)
    if viewController is YourViewControllerClass {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if let controller = storyboard.instantiateViewController(withIdentifier: "storyboardID") as? YourViewControllerClass {
            controller.modalPresentationStyle = .fullScreen
            self.present(controller, animated: true, completion: nil)
        }

        return false
    }

    // Tells the tab bar to select other view controller as normal
    return true
}
Run Code Online (Sandbox Code Playgroud)

我没有测试上面的代码,因为我的实现略有不同,并且有更多的变量.一般原则是一样的.

让我知道你如何继续,如有必要,我会更新答案.


Ahm*_*d F 5

假设您符合UITabBarControllerDelegate,您可以实现:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

    // here, you should edit "0" to be matched with your selected item
    // for instance, if there is 5 items and the desired item is in the middle, the compared value should be "2"
    if tabBarController.selectedIndex == 0 {

        // simply, you will need to get the desired view controller and persent it:
        let desiredStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let desiredViewController = desiredStoryboard.instantiateViewController(withIdentifier: "storyboard id")

        present(desiredViewController, animated: true, completion: nil)

    }
}
Run Code Online (Sandbox Code Playgroud)