创建Camera视图作为另一个视图的叠加?

时间:2018-01-25 11:46:23

标签: ios swift uiviewcontroller camera

我有一个QR码扫描器viewcontroller,而不是在导航控制器中推送它我想知道是否有可能将视图控制器实例化为我前一个主屏幕上的覆盖占据屏幕的四分之一,因为我不需要它是一个完整的单独屏幕。

它有自己的viewcontroller和视图,我只需要以较小的尺寸覆盖它。

没有提供代码,因为这更像是一个理论问题

1 个答案:

答案 0 :(得分:1)

我假设您要以编程方式添加一个容器,其中可以在当前UIViewController中添加第二个UIViewController。请参阅有关如何实现此目的的示例。

import UIKit

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set a yellow background color on the parentViewController
        self.view.backgroundColor = .yellow

        // Create our detailViewController which will be contained in the parent
        let detailViewController = DetailViewController()

        // Add this detailViewController as a child in the current ParentViewController
        addChildViewController(detailViewController)

        // Add the detailViewController view as a subview on the ParentViewController
        view.addSubview(detailViewController.view)

        // Since we dont use IB we disable this property to allow programmatic constraints
        detailViewController.view.translatesAutoresizingMaskIntoConstraints = false

        // We create the constraints for our detailViewControllers view
        NSLayoutConstraint.activate([
            detailViewController.view.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            detailViewController.view.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            detailViewController.view.heightAnchor.constraint(equalToConstant: 200),
            detailViewController.view.widthAnchor.constraint(equalToConstant: 200)
            ])
    }
}

class DetailViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .green

        print("DetailViewController loaded!")
    }

}

我们有ParentViewController,背景为黄色。在此控制器中,我们添加绿色DetailViewController作为容器。结果是:

result

当您运行此应用时,您会注意到控制台会打印DetailViewController的结果。

如果您要删除DetailViewController:      //在ParentViewController中调用它      let vc = self.childViewControllers.last             VC?.view.removeFromSuperview()             VC?.removeFromParentViewController()