Make the custom viewcontroller cover the navigation bar

Suj*_*jal 1 view uinavigationbar viewcontroller ios swift

I am showing a custom pop up over my main viewcontroller. For this I have created a viewcontroller in the storyboard (image shown), the corresponding class being as below.

在此处输入图片说明

class PopUpViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        self.showAnimate()
    }

    func showAnimate()
    {
        self.view.alpha = 1.0
    }

    func removeAnimate()
    {
        UIView.animate(withDuration: 0.0, animations: {
            self.view.alpha = 0.0;
        }, completion:{(finished : Bool)  in
            if (finished) {
                self.view.removeFromSuperview()
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

Then in my main viewcontroller, I show this pop up on a button click as follows:

let popOverVC = UIStoryboard(name: "MainViewController", bundle: nil).instantiateViewController(withIdentifier: "popup") as! PopUpViewController
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.bounds
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
Run Code Online (Sandbox Code Playgroud)

This makes the background of the main view controller black with opacity of 70% when the pop up is added. How can I make the navigation bar also have the same background effect?

I have tried updating:

self.view.window?.backgroundColor = UIColor.black.withAlphaComponent(0.7)
Run Code Online (Sandbox Code Playgroud)

and

self.navigationController?.navigationBar.backgroundColor = UIColor.black.withAlphaComponent(0.7)
Run Code Online (Sandbox Code Playgroud)

in viewDidLoad() but did not work. Any possible solution?

Mil*_*sáľ 5

如果我正确理解,则表示您正在将popOverVC子视图添加mainViewController为嵌入其中的视图UINavitationController。如果是这种情况,则逻辑popOverVC上不要覆盖navigationBar 是合理的,因为navigationBar是的子视图,而navigationController不是您的的子视图mainViewController。为了也可以覆盖navigationBar,您必须将其添加popOverVC到中navigationController

// to make things a bit easier working with the optional self.navigationController
guard let navController = self.navigationController else { return }

let popOverVC = UIStoryboard(name: "MainViewController", bundle: nil).instantiateViewController(withIdentifier: "popup") as! PopUpViewController
navController.addChildViewController(popOverVC)
popOverVC.view.frame = navController.view.bounds
navController.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: navController)
Run Code Online (Sandbox Code Playgroud)