iosswiftuinavigationcontrolleruimodalpresentationstyle

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


How to show image in overCurrentContext view in the ViewController2.swift class ? I am doing this correctly when i worked manually but not doing when i using this programmatically in swift.

ViewController1.swift code -

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

ViewController2.swift code -

override func viewDidLoad() {
        super.viewDidLoad()

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

    }

I want this background alpha 0.7 and using .overCurrentContext

Output -

Ouput


Solution

  • You are pushing your viewController, Instead you should present it with modalPresentationStyle .overCurrentContext. Do something like this:

    @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)
    }
    

    For more information you can read more about modalPresentationStyle in Apple Docs.