ios - 从选项卡栏 Controller 以模态方式呈现 View

标签 ios swift popup uitabbarcontroller

如何从标签栏 Controller 中以模态方式呈现 View ,以便 View 覆盖实际 View ?

我想用相机构建一个 View 。就像“WhatsApp”或“Ins​​tagram”一样,中间有一个按钮,用户可以单击该按钮并显示相机 View 。

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

这就是我的 ViewController 连接到 TabBarController 的方式: enter image description here

最佳答案

我不得不在我目前正在构建的应用程序中实现类似的东西,它相对简单,您需要实现 UITabBarController 的委托(delegate)方法为了实现这一目标。

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

从此方法返回 false 将阻止选项卡 Controller 选择您的选项卡,然后您只需要实现自己的逻辑来呈现 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
}

我没有测试上面的代码,因为我的实现略有不同并且有更多变量。大体原理相同。

让我知道您的进展情况,如有必要,我会更新答案。

关于ios - 从选项卡栏 Controller 以模态方式呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46071125/

相关文章:

ios - 来自 NSArray 的 NSMutableArray

iphone - 如何在 iOS 中构建这个屏幕?

arrays - 从indexpath.row中的数组获取特定值

ios - 当动画 View 高度改变时,UIView 圆角断裂

android - 如何设置actionBar弹出背景?

javascript - 如何防止打开 IE 中的默认选项?

ios - 尝试查找相机名称时,Swift 3和AVCaptureDevice贬值

ios - 如何从 UIView 捕获高分辨率图像

ios - 类型不符合协议(protocol) 'ImagePickerDelegate'

javascript - 在通过 child.print() 打印之前,如何等待图像加载到新窗口中?