繁体   English   中英

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

[英]Present a View modally from a tab bar controller

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

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

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

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

我必须在我正在构建的应用程序中实现类似的东西,这是相对简单的,你需要实现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
}

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

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

假设您符合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)

    }
}
    let modalVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifier") 
    modalVC.modalTransitionStyle = .crossDissolve
    modalVC.modalPresentationStyle = .full or .overfullscreen // please check which of the options work
    self.present(modalVC, animated: true, completion: {

    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM