1

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.

enter image description here

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?

2
  • Did you try like that self.navigationController?.navigationBar.backgroundColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.7) ? Commented Feb 28, 2018 at 15:37
  • @Ahmet Ustem I had tried like that as well but does not work. I have got the solution below. Thanks. Commented Feb 28, 2018 at 16:02

1 Answer 1

5

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) 
Sign up to request clarification or add additional context in comments.

3 Comments

May I request you to update popOverVC.view.frame = self.navController.view.bounds with popOverVC.view.frame = navController.view.bounds Your solution works, thanks a lot :)
@MilanNosáľ I implemented your solution and its working. However, after dismissing the subview, the button (on nav bar) clicks are working but the views are not changing accordingly. How can i solve this?
@KushalShrestha did you remove the popOverVC.view from superview? that's only thing that comes to my mind now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.