Can't add a UIView loaded from XIB as a subView of a ViewController view

I want to implement a popup notification into my app when data was being updated successfully or not. To do that I:

  1. created a .xib file: Screenshot - https://cln.sh/HvEHiX
  2. created a class where I load that NIB:
 import UIKit

  class PopupView: UIView {

 static let instance = PopupView()

 @IBOutlet weak var backgroundView: UIView!
 @IBOutlet weak var popupView: UIVisualEffectView!
 @IBOutlet weak var symbol: UIImageView!
 @IBOutlet weak var titleLabel: UILabel!
 @IBOutlet weak var descriptionLabel: UILabel!

override init(frame: CGRect) {
 super.init(frame: frame)
 Bundle.main.loadNibNamed("PopupView", owner: self)
 popupView.layer.cornerRadius = 20
}

required init?(coder: NSCoder) {
 fatalError("init(coder:) has not been implemented")
}

func showPopup(title: String, message: String, symbol: UIImage, on viewController: UIViewController) {
 self.titleLabel.text = title
 self.descriptionLabel.text = message
 self.symbol.image = symbol

 guard let targetView = viewController.view else { return }

 backgroundView.frame = targetView.bounds
 targetView.addSubview(backgroundView)
}
  1. In the above class I created a showPopup method where defined a backgroundView frame to be equal to ViewController bounds.

When I call that method in desired ViewController I receive the behaviour where my popupView shows itself and then went off the screen straight away (black area in the GIF): GIF - https://cln.sh/e4ukf4

Would you be so kind to help me understand and fix the reason why the popupView went off the screen and not just equal to a ViewController bounds.

The implementation is wrong. See links below especially the medium link

https://developer.apple.com/documentation/uikit/mac_catalyst/uikit_catalog_creating_and_customizing_views_and_controls https://medium.com/swift2go/swift-custom-uiview-with-xib-file-211bb8bbd6eb

Where do you call showPopup ?

I call it in needed VC as Popup.instance.showPopup

Can't add a UIView loaded from XIB as a subView of a ViewController view
 
 
Q