如何在 ViewController2.swift class 的 overCurrentContext 视图中显示图像?

How to show image in overCurrentContext view in the ViewController2.swift class?

如何在 ViewController2.swift class 的 overCurrentContext 视图中显示图像?我在手动工作时正确地执行此操作,但在 swift.

中以编程方式使用它时却没有执行此操作

ViewController1.swift代码-

    @IBAction func btnImage(_ sender: Any)
        {
            let imageVC = self.storyboard?.instantiateViewController(withIdentifier: "ImageVC") as! ImageVC
            self.navigationController?.pushViewController(imageVC, animated: true)
        }

ViewController2.swift代码-

override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.setNavigationBarHidden(true, animated: true)
        self.navigationController?.modalPresentationStyle = .overCurrentContext

    }

我想要这个背景 alpha 0.7 并使用 .overCurrentContext

输出-

您正在推动您的 viewController,您应该使用 modalPresentationStyle .overCurrentContext 来展示它。做这样的事情:

@IBAction func btnImage(_ sender: Any)
    {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "ImageVC") as! ImageVC
    vc?.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
    vc?.modalPresentationStyle = .overCurrentContext
    vc?.navigationController?.isNavigationBarHidden = true
    self.navigationController?.present(vc, animated: true, completion: nil)
}

有关更多信息,您可以在 Apple 文档中阅读有关 modalPresentationStyle 的更多信息。 希望对你有帮助!!