60

I want to place a UIView over the entire screen (including the navigation bar). This view will be black with 0.3 opacity. I want to do this to darken out the screen content and push a view on top of this. I am using this code:

UIApplication.sharedApplication().keyWindow?.addSubview(darkView)

This covers the whole screen as expected. However I now want to place another view on top of this dark view. Is there a way to do this? Everything I try just results in the view being under the dark view. Any pointers would be really appreciated! thanks

Kex
  • 8,023
  • 9
  • 56
  • 129

5 Answers5

102

It's really simple.

You just add another view to window! And it will be there, on top of the first view you added. For example, this code adds a black view and a white view:

let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v)
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
window.addSubview(v2)

You can also add the new view as a sub view of the first view you added:

let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v)
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
v.addSubview(v2)

Swift 4

let window = UIApplication.shared.keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v)
v.backgroundColor = .black
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.white
v.addSubview(v2)

Simple!

emrcftci
  • 3,355
  • 3
  • 21
  • 35
Sweeper
  • 213,210
  • 22
  • 193
  • 313
24

For SWIFT 3 use this:

let window = UIApplication.shared.keyWindow!
window.addSubview(someView)
Mario Jaramillo
  • 567
  • 6
  • 9
8

Swift 5:

let window = UIApplication.shared.windows.last!

let viewToShow = UIView(frame: CGRect(x: 0, y: 0, width: window.frame.size.width, height: 40.0))

viewToShow.backgroundColor = UIColor.white

window.addSubview(viewToShow)
Haseeb Javed
  • 1,769
  • 17
  • 20
7

Swift 4, Adding a UIViewController as a subview to UIWindow

This code is for adding view controller as a subview, covering the whole window with simple animation.

let appDelegate = UIApplication.shared.delegate as! AppDelegate
var customReviewPopup = ReviewPopupViewController.init(nibName: "ReviewPopupViewController", bundle: Bundle.main)

self.appDelegate.window?.addSubview((customReviewPopup.view)!)
self.customReviewPopup.view.frame = (self.appDelegate.window?.bounds)!
self.customReviewPopup.view.alpha = 0
self.customReviewPopup.view.isHidden = true

UIView.animate(withDuration: 0.3, delay: 0, options: .transitionCrossDissolve, animations: {
    self.customReviewPopup.view.isHidden = false
    self.customReviewPopup.view.alpha = 1
}, completion: nil)
Jack Gore
  • 3,874
  • 1
  • 23
  • 32
Pramod More
  • 1,220
  • 2
  • 22
  • 51
  • After showing the alert view, when we want to show the popupwindowView, that is misplacing. – Anil Oct 03 '19 at 19:09
0
UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.windows.last  //This is better

UIApplication.shared.windows.last! //This will some times has bug, because system will add other scene into Application in runtime, for example add subview in UIImagePickerController`