Cover entire screen with UIView using Swift programmatically

I am trying to get a UIView to cover the full screen so that when I press a button, I can apply a dark mode theme. The code I found works in the tutorial I have seen, but not in my application. I think Swift may have updated the language, making the solution obsolete.

if let window = UIApplication.shared.keyWindow {
    let blackView = UIView()
    blackView.backgroundColor = .black
    view.addSubview(blackView)
    blackView.frame = window.frame
}

The issue is that the view property in view.addSubview(blackView) should be window instead. The updated code is:

if let window = UIApplication.shared.keyWindow {
    let blackView = UIView()
    blackView.backgroundColor = .black
    window.addSubview(blackView)
    blackView.frame = window.frame
}