Nikki
Nikki

Reputation: 192

Add subview on the top of every ViewController

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.

Upvotes: 2

Views: 1341

Answers (2)

zzzwco
zzzwco

Reputation: 230

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!
  }
}

Upvotes: 3

Chirag Sondagar
Chirag Sondagar

Reputation: 619

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

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

Upvotes: 0

Related Questions