Reputation: 1507
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()
}
});
}
}
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)
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)
and
self.navigationController?.navigationBar.backgroundColor = UIColor.black.withAlphaComponent(0.7)
in viewDidLoad() but did not work. Any possible solution?
Upvotes: 1
Views: 1322
Reputation: 19737
If I understand it correctly, you are adding the popOverVC
as a subview to the view of your mainViewController
that is embedded in UINavitationController
. If that is the case, then it is only logical that the popOverVC
does not overlay the navigationBar, because navigationBar is a subview of the navigationController
, not of your mainViewController
. To be able to overlay also the navigationBar, you will have to add that popOverVC
to the 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)
Upvotes: 5