skip to Main Content

I want to place a Custom UIView over every view controller screen. I want to do this to show the download progress of video. I create a custom view using xib and use this code -:

 let window = UIApplication.shared.keyWindow!
    window.subviews(CustomView)

but this code is not working how to resolve this issue.

2

Answers


  1. You can write this code to add the subview in the viewcontroller.

    let window = UIApplication.shared.keyWindow!
    window.addSubview(CustomView)
    
    Login or Signup to reply.
  2. UIApplication.topWindow.addSubview(CustomView)
    
    extension UIApplication {
      
      static var topWindow: UIWindow {
        if #available(iOS 15.0, *) {
          let scenes = UIApplication.shared.connectedScenes
          let windowScene = scenes.first as? UIWindowScene
          return windowScene!.windows.first!
        }
        return UIApplication.shared.windows.filter { $0.isKeyWindow }.first!
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search