简体   繁体   English

从选项卡栏控制器以模态方式呈现视图控制器

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

I want to build a view with a camera.我想用相机构建一个视图。 Something just like Instagram where there is a button in the middle that the user can click and the camera view shows up.就像 Instagram 一样,中间有一个按钮,用户可以点击它并显示相机视图。 I implemented a code for the TabViewController in the AppDelegate but nothing happens, no animation or Presentation of the new ViewController .我在AppDelegateTabViewController实现了一个代码,但没有任何反应,没有动画或新ViewController演示。

Here is my AppDelegate:这是我的 AppDelegate:

import UIKit


class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {
    var window: UIWindow?
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: ViewController) -> Bool {
    if viewController is ViewController {
        let storyboard = UIStoryboard(name: "Main.storyboard", bundle: nil)
        if let controller = storyboard.instantiateViewController(withIdentifier: "cameraVC") as? ViewController {
            controller.modalPresentationStyle = .fullScreen
            tabBarController.present(controller, animated: true, completion: nil)
        }
        return false
    }
    return true
}

Here is my Storyboard:这是我的故事板:

主故事板

Any ideas?有什么想法吗?

I suggest to create a custom class for your TabBarController, then assign the delegate to that.我建议为您的 TabBarController 创建一个自定义类,然后将委托分配给它。

You can either assign and check the restorationIdentifier of the view controller, or do a type check.您可以分配和检查视图控制器的restorationIdentifier ,或者进行类型检查。 I usually use storyboard identifier as the restoration identifier of the view controller(s).我通常使用故事板标识符作为视图控制器的恢复标识符。

class TabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if let identifier = viewController.restorationIdentifier, identifier == "cameraVC" {
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "cameraVC") as! CameraViewController
            present(vc, animated: true, completion: nil)
            return false
        }

        return true
    }
}

Here's a sample you can play with: https://gist.github.com/emrekyv/3343aa40c24d7e54244dc09ba0cd95df这是您可以使用的示例: https : //gist.github.com/emrekyv/3343aa40c24d7e54244dc09ba0cd95df

I just tried and It worked perfectly for me:我刚刚尝试过,它对我来说非常有效:

Create a Custom class for your TabBarController and assign it to your Controller in Storyboard.为您的 TabBarController 创建一个自定义类,并将其分配给 Storyboard 中的控制器。

After that override didSelect of tabBarController and write your presentation code there :之后覆盖 tabBarController 的 didSelect 并在那里编写您的演示文稿代码:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

      if let controller = self.viewControllers?[self.selectedIndex] as? ViewController {

          controller.modalPresentationStyle = .fullScreen
          self.present(controller, animated: true, completion: nil
       }
}

Hope it helps!!希望有帮助!!

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

相关问题 从标签栏控制器以模态方式呈现视图 - Present a View modally from a tab bar controller 在选项卡栏控制器中选择时以模态显示视图控制器 - Present a View Controller modally when selected in Tab Bar Controller 如何在选项卡栏项目上轻按以方式显示View Controller - How to modally present a View Controller on tap of tab bar item 嵌入在导航控制器中的当前View Controller(以选项卡栏控制器的形式) - Present View Controller embedded in Navigation Controller apart of Tab bar controller modally 当视图 Controller 嵌入导航 Controller 时,以模态方式显示选项卡栏视图? - Present a Tab Bar View Modally when View Controller is embedded in a Navigation Controller? 从xib文件以模态形式显示View Controller - Present View Controller modally from xib file 如何不显示查看 Controller 模态 - How to not present View Controller Modally 如何从UI视图控制器显示选项卡栏控制器 - How to present a Tab Bar Controller from a UI View Controller 关闭以模态显示的视图使标签栏控制器(种类)重置 - Dismiss Modally presented view makes tab bar controller (kind of) reset Swift:轻按Tab栏时关闭模态呈现的视图控制器 - Swift: Dismissing Modally Presented View Controller when Tab Bar is tapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM