4

How to make the UIView to cover the whole screen. I have a black UIView and it's alpha value is 0.5. It has a subview on it as well. Right now the blackView is below the navigationBar. I want it to cover the whole screen including navigationBar.

As you can see in the picture below. The dark view behind the popup view should cover the whole screen. The constraints I am using for the view is simply:

view.addsubView(blackView)

_ = blackview.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)

The pop up window is a subview of the blackView:

blackView.addSubview(reminderPopUpView) 

How can I do this?

brandonscript
  • 68,675
  • 32
  • 163
  • 220
Osama Naeem
  • 1,830
  • 5
  • 16
  • 34

4 Answers4

8

You can simply add your black view as a window subview.

UIApplication.shared.keyWindow?.addSubview(blackView)
Lauri Nurmi
  • 566
  • 1
  • 3
  • 14
Savca Marin
  • 427
  • 4
  • 11
4

Add your view to subview of window like following:

let mainWindow = UIApplication.sharedApplication().keyWindow!
let overlayview = UIView(frame: CGRect(x: mainWindow.frame.origin.x, y: mainWindow.frame.origin.y, width: mainWindow.frame.width, height: mainWindow.frame.height))
mainWindow.addSubview(overlayview);

you may adjust its background color and alpha based on your requirements.

valosip
  • 3,167
  • 1
  • 14
  • 26
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
4

Follow below steps to resolve your problem:

1> Create your UIView with outlet you want to overlay lets call it viewOverLay

2> Use this below code to overlay that view to cover whole screen even navigation bar

self.navigationController?.view.addSubview(self.viewOverLay) 
self.viewOverLay.frame.size.height = (self.navigationController?.view.bounds.height)! 
self.viewOverLay.alpha = 0.0

3> When you want to show it

UIView.animate(withDuration: 0.3) { self.viewOverLay.alpha = 1.0 } 

4> When you want to hide it

UIView.animate(withDuration: 0.3) { self.viewOverLay.alpha = 0.0 }

Please let me know if it works for you.

Patrick R
  • 6,621
  • 1
  • 24
  • 27
  • Giving me error that UIView doesn't have "height" as member – Osama Naeem Jan 13 '18 at 11:21
  • please see updated answer, I have modified a code in step2 – Patrick R Jan 13 '18 at 11:24
  • Thanks a lot! it's working now. I have one more issue now. Since the pop up box that I wanted to show on the viewOverLay contains buttons, now whenever I press those buttons, they are not responding. What could be the issue? For example, when I press "Remove" or "Done" - their handler functions are not called. Please help! – Osama Naeem Jan 13 '18 at 11:56
  • Thanks, for new issue, check your view hierarchy it will have same like 1> Overlay View withIBOUTLET Your BlackView 2> in BlackView add your reminderPopUpView view then it will sure work. Also please mark answer as selected as it seems working for now! – Patrick R Jan 13 '18 at 12:40
  • The view hierarchy is correct. I added a tap gesture recognizer on BlackView to dismiss popupview and blackview. The gesture seems to work even if popupview is tapped but the datepicker in the popupview seems to work just fine. I am confused and frustrated. – Osama Naeem Jan 13 '18 at 12:42
3

In my case it was easier to draw the popupView on a new UIViewController using autolayout, then I show it with a fullscreen fade in:

if let popupVC = UIStoryboard(name: "Popup", bundle: nil).instantiateViewController(withIdentifier: "PopupVC") as? PopupVC {
        popupVC.parentVC = self
        popupVC.modalPresentationStyle = UIModalPresentationStyle.custom
        popupVC.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
        present(popupVC, animated: true, completion: nil)
}

With the line popupVC.parentVC = self I'm passing the current UIViewController (called parentVC) to the popupVC. In this way when the popupVC is dismissed I can pass data back to its parent. The same can be done using a delegate, it depends on how much decoupling is worth for you.

ivoroto
  • 925
  • 12
  • 12